Пример #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
 void addHeaders(HttpURLConnection conn) {
   for (String key : headers.keySet()) conn.setRequestProperty(key, headers.get(key));
 }