@Override
  public HttpResponseMessage execute(HttpMessage request, Map<String, Object> httpParameters)
      throws IOException {
    // Setup the connection.
    HttpURLConnection connection = (HttpURLConnection) request.url.openConnection();
    connection.setRequestMethod(request.method);
    for (Entry<String, String> header : request.headers) {
      connection.setRequestProperty(header.getKey(), header.getValue());
    }
    InputStream messageBodyStream = request.getBody();
    boolean doOutput =
        messageBodyStream != null
            && (HTTP_POST_METHOD.equalsIgnoreCase(request.method)
                || HTTP_PUT_METHOD.equalsIgnoreCase(request.method));
    if (doOutput) {
      connection.setDoOutput(true);
    }
    connection.connect();

    // Send the request body.
    if (doOutput) {
      Writer outputWriter = new OutputStreamWriter(connection.getOutputStream());
      outputWriter.write(readInputStream(messageBodyStream));
      outputWriter.flush();
      outputWriter.close();
    }

    // Return the response stream.
    return new HttpResponse(
        request.method, request.url, connection.getResponseCode(), connection.getInputStream());
  }
示例#2
0
 /**
  * Construct an HTTP request from this OAuth message.
  *
  * @param style where to put the OAuth parameters, within the HTTP request
  * @deprecated use HttpMessage.newRequest
  */
 public HttpMessage toHttpRequest(OAuthClient.ParameterStyle style) throws IOException {
   return HttpMessage.newRequest(this, style.getReplacement());
 }