当前位置: 首页>>代码示例>>Java>>正文


Java URLEncoder.encode方法代码示例

本文整理汇总了Java中java.net.URLEncoder.encode方法的典型用法代码示例。如果您正苦于以下问题:Java URLEncoder.encode方法的具体用法?Java URLEncoder.encode怎么用?Java URLEncoder.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLEncoder的用法示例。


在下文中一共展示了URLEncoder.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testQueryChineseAndSpaceAndEmpty

import java.net.URLEncoder; //导入方法依赖的package包/类
@Test
public void testQueryChineseAndSpaceAndEmpty() throws UnsupportedEncodingException {
  String strUri =
      "cse://1.1.1.1:1234/abc?a=1&b=&country=" + URLEncoder.encode("中 国", StandardCharsets.UTF_8.name());
  URIEndpointObject ep = new URIEndpointObject(strUri);

  Map<String, List<String>> querys = Deencapsulation.getField(ep, "querys");
  Assert.assertEquals(3, querys.size());

  Assert.assertEquals(1, ep.getQuery("a").size());
  Assert.assertEquals("1", ep.getFirst("a"));

  Assert.assertEquals(1, ep.getQuery("b").size());
  Assert.assertEquals("", ep.getFirst("b"));

  Assert.assertEquals(1, ep.getQuery("country").size());
  Assert.assertEquals("中 国", ep.getFirst("country"));
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:19,代码来源:TestURIEndpointObject.java

示例2: doGet

import java.net.URLEncoder; //导入方法依赖的package包/类
/**
 * Perform a GET request
 * @param url the URL relative to the base URL
 * @param props additional query parameters
 * @return a List of Strings, one for each line in the response
 * @throws IOException
 */
public List<String> doGet(String url,
                          Properties props) throws IOException {
  url = baseURL + url;
  if (props != null) {
     url += "?"; //$NON-NLS-1$
    for (Enumeration<?> e = props.keys(); e.hasMoreElements();) {
      String key = (String) e.nextElement();
      String value = props.getProperty(key);
      url += key + "=" + URLEncoder.encode(value,"UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
      if (e.hasMoreElements()) {
        url += "&"; //$NON-NLS-1$
      }
    }
  }

  final URLConnection conn = new URL(url).openConnection();
  conn.setUseCaches(false);
  return readLines(conn.getInputStream());
}
 
开发者ID:ajmath,项目名称:VASSAL-src,代码行数:27,代码来源:HttpRequestWrapper.java

示例3: getSelectedServiceKeyQueryPart

import java.net.URLEncoder; //导入方法依赖的package包/类
public String getSelectedServiceKeyQueryPart(String selectedServiceKey) {
    String selectedService = "";
    ExternalContext extContext = getFacesContext().getExternalContext();
    String charEncoding = extContext.getRequestCharacterEncoding();

    try {
        String name = URLEncoder.encode("selectedServiceKey", charEncoding);
        String value = URLEncoder.encode(selectedServiceKey, charEncoding);
        selectedService = '?' + name + '=' + value;
    } catch (UnsupportedEncodingException e) {
        extContext.log(getClass().getName()
                + ".getSelectedServiceQueryPart()", e);

    }
    return selectedService;
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:17,代码来源:UiDelegate.java

示例4: doSetCookie

import java.net.URLEncoder; //导入方法依赖的package包/类
/**
 * 设置Cookie的值,并使其在指定时间内生效
 * 
 * @param cookieMaxage cookie生效的最大秒数
 */
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
        String cookieName, String cookieValue, int cookieMaxage, String encodeString) {
    try {
        if (cookieValue == null) {
            cookieValue = "";
        } else {
            cookieValue = URLEncoder.encode(cookieValue, encodeString);
        }
        Cookie cookie = new Cookie(cookieName, cookieValue);
        if (cookieMaxage > 0)
            cookie.setMaxAge(cookieMaxage);
        if (null != request)// 设置域名的cookie
            cookie.setDomain(getDomainName(request));
        cookie.setPath("/");
        response.addCookie(cookie);
    } catch (Exception e) {
        logger.error("Cookie Encode Error.", e);
    }
}
 
开发者ID:jthinking,项目名称:linux-memory-monitor,代码行数:25,代码来源:CookieUtils.java

示例5: createFromFilePath

import java.net.URLEncoder; //导入方法依赖的package包/类
public static UrlEncodedString createFromFilePath(String path)
{
	try
	{
		path = URLEncoder.encode(path, UTF_8);
	}
	catch( UnsupportedEncodingException e )
	{
		throw new RuntimeException(e);
	}
	// Ensure forward slashes are still slashes
	path = path.replaceAll("%2F", "/"); //$NON-NLS-1$ //$NON-NLS-2$

	// Ensure that pluses are changed into the correct %20
	path = path.replaceAll("\\+", "%20"); //$NON-NLS-1$ //$NON-NLS-2$
	return new UrlEncodedString(path);
}
 
开发者ID:equella,项目名称:Equella,代码行数:18,代码来源:UrlEncodedString.java

示例6: getCoreQuery

import java.net.URLEncoder; //导入方法依赖的package包/类
String getCoreQuery(String verb, String userId, String userIP, String sToken) throws ServletException {
	try {
		JSONObject json = new JSONObject();
		json.put(ICredentialsMicroformat.VERB, verb);
		json.put(ICredentialsMicroformat.USER_ID, userId);
		json.put(ICredentialsMicroformat.SESSION_TOKEN, sToken);
		json.put(ICredentialsMicroformat.USER_IP, userIP);
		String result = json.toJSONString();
		result = URLEncoder.encode(result, "UTF-8");
		json = null;
		return result;
	} catch (Exception e) {
		environment.logError(e.getMessage(), e);
		throw new ServletException(e);
	}
}
 
开发者ID:topicquests,项目名称:backside-servlet-ks,代码行数:17,代码来源:GUIHandler.java

示例7: makeLink

import java.net.URLEncoder; //导入方法依赖的package包/类
/**
 * Crea un token-link unendo le due componenti passate.
 * @param id
 * @param token
 * @param linkParameters name-value paris of url parameters to add at the end - can be null or empty
 * @return una stringa <ID>-<token>
 */
public String makeLink(long id, long token, Map<String, String> linkParameters) {
	StringBuilder result = new StringBuilder(id + "-" + token).append("?");
	if (linkParameters!=null) {
		try {
			for (String key : linkParameters.keySet()) {
				String name = URLEncoder.encode(key, "UTF-8");
				String value = URLEncoder.encode(linkParameters.get(key), "UTF-8");
				result.append(name).append("=").append(value).append("&");
			}
		} catch (UnsupportedEncodingException e) {
			log.error("Impossible error occurred", e);
		}
	}
	String link = result.toString();
	link = StringUtils.removeEnd(link, "&");
	link = StringUtils.removeEnd(link, "?");
	return link;
}
 
开发者ID:xtianus,项目名称:yadaframework,代码行数:26,代码来源:YadaTokenHandler.java

示例8: urlEncode

import java.net.URLEncoder; //导入方法依赖的package包/类
private String urlEncode(String value) {
	try {
		return URLEncoder.encode(value, "UTF-8");
	}
	catch (UnsupportedEncodingException e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:zhaojunfei,项目名称:lemon,代码行数:9,代码来源:CookieHttpSessionStrategy.java

示例9: addUserCookie

import java.net.URLEncoder; //导入方法依赖的package包/类
/**
 * 添加用户信息到Cookie
 * 
 * @param user
 *            用户信息
 * @return Cookie信息
 */

public static Cookie addUserCookie(TUser user) {
    try {
        Cookie cookie = new Cookie(USER_COOKIE, URLEncoder.encode(user.getLoginid(), YiDuConstants.ENCODING_UTF_8)
                + "," + user.getPassword() + "," + user.getType() + "," + StringUtils.isNotBlank(user.getOpenid()));
        // 默认保持两年cookie保存两周
        cookie.setMaxAge(60 * 60 * 24 * 365);
        return cookie;
    } catch (UnsupportedEncodingException e) {
        logger.error(e);
    }
    return null;
}
 
开发者ID:luckyyeah,项目名称:YiDu-Novel,代码行数:21,代码来源:CookieUtils.java

示例10: doPost

import java.net.URLEncoder; //导入方法依赖的package包/类
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
    String fname = URLEncoder.encode(request.getParameter("fname"), "ISO-8859-1");
    String lname = URLEncoder.encode(request.getParameter("lname"), "ISO-8859-1");
    newCookie(response, "fname", fname);
    newCookie(response, "lname", lname);

    System.out.println("doPost " + fname + " " + fname);

    response.sendRedirect(request.getContextPath());

}
 
开发者ID:181192,项目名称:DAT104,代码行数:14,代码来源:RegistrerServlet.java

示例11: encode

import java.net.URLEncoder; //导入方法依赖的package包/类
public static String encode(String data) {
    try {
        return URLEncoder.encode(data, UTF8);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("unknown charset:"+UTF8);
    }
}
 
开发者ID:TFdream,项目名称:okurl,代码行数:8,代码来源:Util.java

示例12: urlEncode

import java.net.URLEncoder; //导入方法依赖的package包/类
private static String urlEncode(String s) {
	try {
		return URLEncoder.encode(s, "UTF-8");
	} catch (UnsupportedEncodingException e) {
		return s;
	}
}
 
开发者ID:21ca,项目名称:selenium-testng-template,代码行数:8,代码来源:DownloadUtils.java

示例13: setBaseURL

import java.net.URLEncoder; //导入方法依赖的package包/类
public void setBaseURL(URL url) {
//		baseScheme = url.getProtocol();
//		baseHost = baseScheme + "://" + url.getHost() + (url.getPort() == -1 ? "" : ":" + url.getPort());
		basePath = (url.getPath().length() == 0 ? "/" : url.getPath()) + (url.getQuery() == null ? "" : "?" + url.getQuery());
		try {
			basePath = URLEncoder.encode(basePath, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
	}
 
开发者ID:convertigo,项目名称:convertigo-engine,代码行数:10,代码来源:UrlBuilder.java

示例14: execute

import java.net.URLEncoder; //导入方法依赖的package包/类
@Override
protected void execute(Input input, Output output) {
  if (input.getArguments().length > 0) {
    String query = StringUtils.arrayToString(input.getArguments(), " ").trim();
    try {
      query = URLEncoder.encode(query, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    output.sendMessage("http://lmgtfy.com/?q=" + query);
  } else {
    output.sendMessage("http://lmgtfy.com/");
  }
}
 
开发者ID:Svetroid,项目名称:Hobbes-v1,代码行数:15,代码来源:Lmgtfy.java

示例15: encodeURN

import java.net.URLEncoder; //导入方法依赖的package包/类
/**
 * Encode a public identifier as a "publicid" URN.
 *
 * @param publicId The unnormalized public identifier
 *
 * @return The normalized identifier
 * @throws CatalogException if encoding failed
 */
static String encodeURN(String publicId) {
    String urn = normalizePublicId(publicId);

    try {
        urn = URLEncoder.encode(urn, "UTF-8");
        urn = urn.replace("::", ";");
        urn = urn.replace("//", ":");
    } catch (UnsupportedEncodingException ex) {
        CatalogMessages.reportRunTimeError(CatalogMessages.ERR_OTHER, ex);
    }
    return Util.URN + urn;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:Normalizer.java


注:本文中的java.net.URLEncoder.encode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。