Example usage for java.net HttpURLConnection getRequestProperty

List of usage examples for java.net HttpURLConnection getRequestProperty

Introduction

In this page you can find the example usage for java.net HttpURLConnection getRequestProperty.

Prototype

public String getRequestProperty(String key) 

Source Link

Document

Returns the value of the named general request property for this connection.

Usage

From source file:twity.twitter.Twitter.java

public static String fetch(String urlString, OAuthConsumer consumer) throws Exception {
    Long start = System.currentTimeMillis();
    URL url = new URL(urlString);
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    consumer.sign(request);/*from   ww w.  j a v a2  s  .c  o m*/

    HTTPRequest req = new HTTPRequest(url, HTTPMethod.GET);
    req.addHeader(new HTTPHeader("Authorization", request.getRequestProperty("Authorization")));

    HTTPResponse resp = fetch.fetch(req);

    Long stop = System.currentTimeMillis();
    System.err.println("Execution time for url:" + urlString + " is=" + (stop - start) + ", code="
            + resp.getResponseCode());

    return new String(resp.getContent());
}

From source file:org.rapidcontext.app.plugin.http.HttpPostProcedure.java

/**
 * Logs the HTTP request to the procedure call context.
 *
 * @param cx             the procedure call context
 * @param con            the HTTP connection
 * @param data           the HTTP request data
 *//*from w w w. j a  va 2 s  .co m*/
private static void logRequest(CallContext cx, HttpURLConnection con, String data) {

    cx.log("HTTP " + con.getRequestMethod() + " " + con.getURL());
    Iterator iter = con.getRequestProperties().keySet().iterator();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        cx.log("  " + key + ": " + con.getRequestProperty(key));
    }
    if (data != null) {
        cx.log(data);
    }
}

From source file:org.apache.hadoop.fs.azure.metrics.ResponseReceivedMetricUpdater.java

/**
 * Get the content length of the request in the given HTTP connection.
 * @param connection The connection./*from w w  w .j  ava  2s. c  o m*/
 * @return The content length, or zero if not found.
 */
private long getRequestContentLength(HttpURLConnection connection) {
    String lengthString = connection.getRequestProperty(HeaderConstants.CONTENT_LENGTH);
    if (lengthString != null) {
        return Long.parseLong(lengthString);
    } else {
        return 0;
    }
}

From source file:com.yahoo.social.sdk.oauth.YahooApplication.java

public String fetch(String uri, String method) {
    try {//from ww  w .j a va  2  s . co m
        HttpURLConnection request = (HttpURLConnection) new URL(uri).openConnection();
        request.setRequestMethod(method);

        consumer.sign(request);
        String auth = request.getRequestProperty("Authorization").trim().replace("OAuth ", "").replace(",", "&")
                .replace("\"", "");

        HTTPRequest req = new HTTPRequest(new URL(uri + "&" + auth),
                "PUT".equals(method) ? HTTPMethod.PUT : HTTPMethod.GET);
        return new String(fetch.fetch(req).getContent());
    } catch (Throwable t) {
        return "";
    }
}

From source file:twity.twitter.Twitter.java

public void updateStatus(String status) {
    Long start = System.currentTimeMillis();
    try {//from www  . ja v a  2 s  .  com
        //Why does sign-post require a HttpURLConnection?
        status = URLEncoder.encode(status);
        URL url = new URL("http://twitter.com/statuses/update.json?status=" + status);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.setRequestMethod("POST");
        consumer.sign(request);

        HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
        req.addHeader(new HTTPHeader("Authorization", request.getRequestProperty("Authorization")));
        fetch.fetch(req);
        cache.delete(guid + ":friends");
    } catch (Exception e) {
        System.err.println("unable to update status: " + e.getMessage());
    }

    Long stop = System.currentTimeMillis();
    System.err.println("Execution time for update status is = " + (stop - start));
}

From source file:com.facebook.GraphRequestTest.java

@Test
public void testSingleGetToHttpRequest() throws Exception {
    GraphRequest requestMe = new GraphRequest(null, "TourEiffel");
    HttpURLConnection connection = GraphRequest.toHttpConnection(requestMe);

    assertTrue(connection != null);/*w  w w . ja  v  a2s.c  om*/

    assertEquals("GET", connection.getRequestMethod());
    assertEquals("/" + ServerProtocol.getAPIVersion() + "/TourEiffel", connection.getURL().getPath());

    assertTrue(connection.getRequestProperty("User-Agent").startsWith("FBAndroidSDK"));

    Uri uri = Uri.parse(connection.getURL().toString());
    assertEquals("android", uri.getQueryParameter("sdk"));
    assertEquals("json", uri.getQueryParameter("format"));
}

From source file:com.microsoft.azure.storage.core.Utility.java

/**
 * Returns the standard header value from the specified connection request, or an empty string if no header value
 * has been specified for the request./*from   ww w .  ja va2s  .c  o  m*/
 * 
 * @param conn
 *            An <code>HttpURLConnection</code> object that represents the request.
 * @param headerName
 *            A <code>String</code> that represents the name of the header being requested.
 * 
 * @return A <code>String</code> that represents the header value, or <code>null</code> if there is no corresponding
 *         header value for <code>headerName</code>.
 */
public static String getStandardHeaderValue(final HttpURLConnection conn, final String headerName) {
    final String headerValue = conn.getRequestProperty(headerName);

    // Coalesce null value
    return headerValue == null ? Constants.EMPTY_STRING : headerValue;
}

From source file:org.scribe.model.ProxyRequest.java

void addBody(final HttpURLConnection conn, final 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);
    }//from ww  w  .ja v  a 2 s .com
    conn.setDoOutput(true);
    conn.getOutputStream().write(content);
}

From source file:com.facebook.RequestTests.java

@LargeTest
public void testExecuteSingleGetUsingHttpURLConnection() throws IOException {
    Bundle parameters = new Bundle();
    parameters.putString("fields", "location");

    GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "TourEiffel", parameters,
            null);/*from ww  w . j  ava2s .c o  m*/
    HttpURLConnection connection = GraphRequest.toHttpConnection(request);

    assertEquals("gzip", connection.getRequestProperty("Content-Encoding"));
    assertEquals("application/x-www-form-urlencoded", connection.getRequestProperty("Content-Type"));

    List<GraphResponse> responses = GraphRequest.executeConnectionAndWait(connection,
            Arrays.asList(new GraphRequest[] { request }));
    assertNotNull(responses);
    assertEquals(1, responses.size());

    GraphResponse response = responses.get(0);

    assertTrue(response != null);
    assertTrue(response.getError() == null);
    assertNotNull(response.getJSONObject());
    assertNotNull(response.getRawResponse());

    JSONObject graphPlace = response.getJSONObject();
    assertEquals("Paris", graphPlace.optJSONObject("location").optString("city"));

    // Make sure calling code can still access HTTP headers and call disconnect themselves.
    int code = connection.getResponseCode();
    assertEquals(200, code);
    assertTrue(connection.getHeaderFields().keySet().contains("Content-Type"));
    connection.disconnect();
}

From source file:com.facebook.RequestTests.java

@LargeTest
public void testBuildsUploadPhotoHttpURLConnection() throws Exception {
    Bitmap image = createTestBitmap(128);

    GraphRequest request = GraphRequest.newUploadPhotoRequest(AccessToken.getCurrentAccessToken(),
            ShareInternalUtility.MY_PHOTOS, image, "Test photo messsage", null, null);
    HttpURLConnection connection = GraphRequest.toHttpConnection(request);

    assertTrue(connection != null);//from w w w.  j a va2  s  .com
    assertNotSame("gzip", connection.getRequestProperty("Content-Encoding"));
    assertNotSame("application/x-www-form-urlencoded", connection.getRequestProperty("Content-Type"));
}