當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。