Example #1
0
 private static String getRequestCookieString(Connection.Request req) {
   StringBuilder sb = new StringBuilder();
   boolean first = true;
   for (Map.Entry<String, String> cookie : req.cookies().entrySet()) {
     if (!first) sb.append("; ");
     else first = false;
     sb.append(cookie.getKey()).append('=').append(cookie.getValue());
     // todo: spec says only ascii, no escaping / encoding defined. validate on set? or escape
     // somehow here?
   }
   return sb.toString();
 }
Example #2
0
 // set up connection defaults, and details from request
 private static HttpURLConnection createConnection(Connection.Request req) throws IOException {
   HttpURLConnection conn = (HttpURLConnection) req.url().openConnection();
   conn.setRequestMethod(req.method().name());
   conn.setInstanceFollowRedirects(false); // don't rely on native redirection support
   conn.setConnectTimeout(req.timeout());
   conn.setReadTimeout(req.timeout());
   if (req.method() == Method.POST) conn.setDoOutput(true);
   if (req.cookies().size() > 0) conn.addRequestProperty("Cookie", getRequestCookieString(req));
   for (Map.Entry<String, String> header : req.headers().entrySet()) {
     conn.addRequestProperty(header.getKey(), header.getValue());
   }
   return conn;
 }