List of usage examples for java.net HttpCookie parse
public static List<HttpCookie> parse(String header)
From source file:org.piwigo.helper.CookieHelper.java
/** * Extract all cookies from a list of headers * * @param headers List of headers//from w ww. j av a 2s . c om * @return Map of cookies */ public static ArrayMap<String, String> extractAll(List<Header> headers) { ArrayMap<String, String> cookies = new ArrayMap<>(); for (Header header : headers) { if (header.getName().equalsIgnoreCase(COOKIE_HEADER)) { List<HttpCookie> httpCookies = HttpCookie.parse(header.getValue()); for (HttpCookie httpCookie : httpCookies) { cookies.put(httpCookie.getName(), httpCookie.getValue()); } } } return cookies; }
From source file:in.rab.ordboken.CookieSerializer.java
public void loadFromString(String json) { try {/*from w w w. j ava2s. c o m*/ JSONObject obj = new JSONObject(json); JSONArray cookieStrings = obj.getJSONArray("cookies"); URI uri = new URI("http://" + mDomain); for (int i = 0; i < cookieStrings.length(); i++) { String cookieString = cookieStrings.getString(i); HttpCookie cookie = HttpCookie.parse(cookieString).get(0); cookie.setPath("/"); cookie.setDomain(mDomain); mCookieStore.add(uri, cookie); } } catch (Exception e) { // No big deal if this fails. } }
From source file:com.codename1.corsproxy.CORSProxy.java
@Override protected void copyProxyCookie(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Header header) {/* w ww . ja v a2s. com*/ List<HttpCookie> cookies = HttpCookie.parse(header.getValue()); String path = servletRequest.getContextPath(); // path starts with / or is empty string path += servletRequest.getServletPath(); // servlet path starts with / or is empty string for (HttpCookie cookie : cookies) { //set cookie name prefixed w/ a proxy value so it won't collide w/ other cookies String proxyCookieName = getCookieNamePrefix() + cookie.getName(); Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue()); servletCookie.setComment(cookie.getComment()); servletCookie.setMaxAge((int) cookie.getMaxAge()); servletCookie.setPath(path); //set to the path of the proxy servlet // don't set cookie domain //servletCookie.setSecure(cookie.getSecure()); servletCookie.setSecure(false); servletCookie.setVersion(cookie.getVersion()); servletResponse.addCookie(servletCookie); } }
From source file:Main.java
public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); if (cookies != null && !cookies.isEmpty()) { StringBuilder cookieHeader = new StringBuilder(); for (String cookie : cookies.values()) { if (cookieHeader.length() > 0) { cookieHeader.append(";"); }/* ww w . java 2 s.c o m*/ cookieHeader.append(cookie); } connection.setRequestProperty("Cookie", cookieHeader.toString()); } connection.setDoInput(true); if (parameters != null && !parameters.isEmpty()) { connection.setDoOutput(true); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(parametersToWWWFormURLEncoded(parameters)); osw.flush(); osw.close(); } if (cookies != null) { for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { if (headerEntry != null && headerEntry.getKey() != null && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) { for (String header : headerEntry.getValue()) { for (HttpCookie httpCookie : HttpCookie.parse(header)) { cookies.put(httpCookie.getName(), httpCookie.toString()); } } } } } Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringWriter w = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); return w.toString(); }
From source file:org.piwik.ResponseData.java
public List<Cookie> getCookies() { List<Cookie> cookies = new ArrayList<Cookie>(); for (String key : headerData.keySet()) { List<String> headerParts = headerData.get(key); StringBuilder cookieInfo = new StringBuilder(); for (String part : headerParts) { cookieInfo.append(part);/* ww w. j a v a 2 s . c o m*/ } if (key == null && cookieInfo.toString().equals("")) { LOGGER.debug("No more headers, not proceeding"); return null; } if (key == null) { LOGGER.debug("The header value contains the server's HTTP version, not proceeding"); } else if (key.equals("Set-Cookie")) { List<HttpCookie> httpCookies = HttpCookie.parse(cookieInfo.toString()); for (HttpCookie h : httpCookies) { Cookie c = new Cookie(h.getName(), h.getValue()); c.setComment(h.getComment()); if (h.getDomain() != null) { c.setDomain(h.getDomain()); } c.setMaxAge(Long.valueOf(h.getMaxAge()).intValue()); c.setPath(h.getPath()); c.setSecure(h.getSecure()); c.setVersion(h.getVersion()); cookies.add(c); } } else { LOGGER.debug("The provided key (" + key + ") with value (" + cookieInfo + ") were not processed because the key is unknown"); } } return cookies; }
From source file:com.github.parisoft.resty.response.http.HttpResponseExtensionImpl.java
@Override public List<HttpCookie> getCookies() throws IOException { if (cookies != null) { return cookies; }//from w w w . ja v a 2 s . c om final Header cookieHeader = httpResponse.getFirstHeader("Set-Cookie"); if (cookieHeader == null) { return cookies = Collections.emptyList(); } try { return cookies = HttpCookie.parse(cookieHeader.toString()); } catch (Exception e) { throw new IOException("Cannot parse response cookie", e); } }
From source file:org.appspot.apprtc.util.AsyncHttpURLConnection.java
void getCookies(HttpsURLConnection connection) { Map<String, List<String>> headerFields = connection.getHeaderFields(); if (headerFields != null) { List<String> cookiesHeader = headerFields.get(COOKIES_HEADER); if (cookiesHeader != null) { for (String cookie : cookiesHeader) { msCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); }/* w w w. jav a 2s . c om*/ } } }
From source file:com.nominanuda.web.http.ServletHelper.java
public void copyResponse(HttpResponse response, HttpServletResponse servletResponse) throws IOException { servletResponse.setStatus(response.getStatusLine().getStatusCode()); for (Header h : response.getAllHeaders()) { if ("Set-Cookie".equals(h.getName()) || "Set-Cookie2".equals(h.getName())) { for (HttpCookie c : HttpCookie.parse(h.getValue())) { servletResponse.addCookie(servletCookie(c)); }/*from www. ja v a 2 s . c o m*/ } else { servletResponse.setHeader(h.getName(), h.getValue()); } } HttpEntity entity = response.getEntity(); if (entity != null) { Header ct = entity.getContentType(); if (ct != null) { servletResponse.setContentType(ct.getValue()); } Header ce = entity.getContentEncoding(); if (ce != null) { servletResponse.setHeader(ce.getName(), ce.getValue()); } long len = entity.getContentLength(); if (len >= 0) { servletResponse.setContentLength((int) len); } ioHelper.pipe(entity.getContent(), servletResponse.getOutputStream(), true, false); } }
From source file:com.subgraph.vega.ui.scanner.ScanExecutor.java
private List<Cookie> getCookieList(List<String> cookieStringList, URI uri) { if (cookieStringList.size() != 0) { ArrayList<Cookie> cookieList = new ArrayList<Cookie>(cookieStringList.size()); for (String cookieString : cookieStringList) { List<HttpCookie> parseList = HttpCookie.parse(cookieString); for (HttpCookie cookie : parseList) { BasicClientCookie cp = new BasicClientCookie(cookie.getName(), cookie.getValue()); cp.setComment(cookie.getComment()); if (cookie.getDomain() != null) { cp.setDomain(cookie.getDomain()); } else { // just set it to the target host for now - may need something slightly less specific cp.setDomain(uri.getHost()); }/*from ww w . jav a 2 s. co m*/ long maxAge = cookie.getMaxAge(); if (maxAge > 0) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.SECOND, (int) maxAge); cp.setExpiryDate(calendar.getTime()); } cp.setPath(cookie.getPath()); cp.setSecure(cookie.getSecure()); cp.setVersion(cookie.getVersion()); cookieList.add(cp); } } return cookieList; } return null; }
From source file:info.aamulumi.sharedshopping.network.RequestSender.java
/** * Send HTTP Request with params (x-url-encoded) * * @param requestURL - URL of the request * @param method - HTTP method (GET, POST, PUT, DELETE, ...) * @param urlParameters - parameters send in URL * @param bodyParameters - parameters send in body (encoded) * @return JSONObject returned by the server *//* w ww .jav a 2s . c o m*/ public JSONObject makeHttpRequest(String requestURL, String method, HashMap<String, String> urlParameters, HashMap<String, String> bodyParameters) { HttpURLConnection connection = null; URL url; JSONObject jObj = null; try { // Check if we must add parameters in URL if (urlParameters != null) { String stringUrlParams = getFormattedParameters(urlParameters); url = new URL(requestURL + "?" + stringUrlParams); } else url = new URL(requestURL); // Create connection connection = (HttpURLConnection) url.openConnection(); // Add cookies to request if (mCookieManager.getCookieStore().getCookies().size() > 0) connection.setRequestProperty("Cookie", TextUtils.join(";", mCookieManager.getCookieStore().getCookies())); // Set request parameters connection.setReadTimeout(5000); connection.setConnectTimeout(5000); connection.setRequestMethod(method); connection.setDoInput(true); // Check if we must add parameters in body if (bodyParameters != null) { // Create a string with parameters String stringBodyParameters = getFormattedParameters(bodyParameters); // Set output request parameters connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(stringBodyParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "fr-FR"); // Send body's request OutputStream os = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(stringBodyParameters); writer.flush(); writer.close(); os.close(); } // Get response code int responseCode = connection.getResponseCode(); // If response is 200 (OK) if (responseCode == HttpsURLConnection.HTTP_OK) { // Keep new cookies in the manager List<String> cookiesHeader = connection.getHeaderFields().get(COOKIES_HEADER); if (cookiesHeader != null) { for (String cookie : cookiesHeader) mCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); } // Read the response String line; BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } // Parse the response to a JSON Object try { jObj = new JSONObject(sb.toString()); } catch (JSONException e) { Log.d("JSON Parser", "Error parsing data " + e.toString()); Log.d("JSON Parser", "Setting value of jObj to null"); jObj = null; } } else { Log.w("HttpUrlConnection", "Error : server sent code : " + responseCode); } } catch (MalformedURLException e) { Log.e("Network", "Error in URL"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // Close connection if (connection != null) connection.disconnect(); } return jObj; }