예제 #1
1
 private Map<String, String> parseHeaders(HttpURLConnection conn) {
   Map<String, String> headers = new HashMap<String, String>();
   for (String key : conn.getHeaderFields().keySet()) {
     headers.put(key, conn.getHeaderFields().get(key).get(0));
   }
   return headers;
 }
예제 #2
0
 Response(HttpURLConnection connection) throws IOException {
   try {
     connection.connect();
     code = connection.getResponseCode();
     headers = parseHeaders(connection);
     stream = isSuccessful() ? connection.getInputStream() : connection.getErrorStream();
   } catch (UnknownHostException e) {
     throw new OAuthException("The IP address of a host could not be determined.", e);
   }
 }
예제 #3
0
  void addBody(HttpURLConnection conn, byte[] content) throws IOException {
    conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length));

    // Set default content type if none is set.
    if (conn.getRequestProperty(CONTENT_TYPE) == null) {
      conn.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
    }
    conn.setDoOutput(true);
    conn.getOutputStream().write(content);
  }
예제 #4
0
 Response doSend() throws IOException {
   connection.setRequestMethod(this.verb.name());
   if (connectTimeout != null) {
     connection.setConnectTimeout(connectTimeout.intValue());
   }
   if (readTimeout != null) {
     connection.setReadTimeout(readTimeout.intValue());
   }
   if (boundary != null) {
     connection.setRequestProperty(CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
   }
   addHeaders(connection);
   if (verb.equals(Verb.PUT) || verb.equals(Verb.POST)) {
     addBody(connection, getByteBodyContents());
   }
   return new Response(connection);
 }
예제 #5
0
 Response doSend(RequestTuner tuner) throws IOException
 {
   connection.setRequestMethod(this.verb.name());
   if (connectTimeout != null) 
   {
     connection.setConnectTimeout(connectTimeout.intValue());
   }
   if (readTimeout != null)
   {
     connection.setReadTimeout(readTimeout.intValue());
   }
   tuner.tune(this);
   addHeaders(connection);
   if (verb.equals(Verb.PUT) || verb.equals(Verb.POST))
   {
     addBody(connection, getByteBodyContents());
   }
   return new Response(connection);
 }
예제 #6
0
 private void createConnection() throws IOException
 {
   String completeUrl = getCompleteUrl();
   if (connection == null)
   {
     System.setProperty("http.keepAlive", connectionKeepAlive ? "true" : "false");
     connection = (HttpURLConnection) new URL(completeUrl).openConnection();
     connection.setInstanceFollowRedirects(followRedirects);
   }
 }
예제 #7
0
 void addHeaders(HttpURLConnection conn)
 {
   for (String key : headers.keySet())
     conn.setRequestProperty(key, headers.get(key));
 }