本文整理汇总了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"));
}
示例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());
}
示例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;
}
示例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);
}
}
示例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);
}
示例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);
}
}
示例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;
}
示例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);
}
}
示例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;
}
示例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());
}
示例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);
}
}
示例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;
}
}
示例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) {
}
}
示例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/");
}
}
示例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;
}