Search in sources :

Example 6 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project nanohttpd by NanoHttpd.

the class CookieIntegrationTest method testNoCookies.

@Test
public void testNoCookies() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8192/");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    this.httpclient.execute(httpget, responseHandler);
    CookieStore cookies = this.httpclient.getCookieStore();
    assertEquals(0, cookies.getCookies().size());
}
Also used : CookieStore(org.apache.http.client.CookieStore) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 7 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostWithNoParameters.

@Test
public void testPostWithNoParameters() throws Exception {
    this.testServer.response = "testPostWithNoParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = this.httpclient.execute(httppost, responseHandler);
    assertEquals("POST:testPostWithNoParameters", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 8 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostRequestWithMultipartEncodedParameters.

@Test
public void testPostRequestWithMultipartEncodedParameters() throws Exception {
    this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/");
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("age", new StringBody("120"));
    reqEntity.addPart("gender", new StringBody("Male"));
    httppost.setEntity(reqEntity);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = this.httpclient.execute(httppost, responseHandler);
    assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) Test(org.junit.Test)

Example 9 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostRequestWithFormEncodedParameters.

@Test
public void testPostRequestWithFormEncodedParameters() throws Exception {
    this.testServer.response = "testPostRequestWithFormEncodedParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/");
    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("age", "120"));
    postParameters.add(new BasicNameValuePair("gender", "Male"));
    httppost.setEntity(new UrlEncodedFormEntity(postParameters));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = this.httpclient.execute(httppost, responseHandler);
    assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Test(org.junit.Test)

Example 10 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project OpenRefine by OpenRefine.

the class RefineBroker method getUserId.

// ----------------------------------------------------------------------------------------
@SuppressWarnings("unchecked")
protected String getUserId(HttpServletRequest request) throws Exception {
    // This is useful for testing
    if (developmentMode) {
        return getParameter(request, "uid");
    }
    String oauth = request.getHeader(DELEGATED_OAUTH_HEADER);
    if (oauth == null) {
        throw new RuntimeException("The request needs to contain the '" + DELEGATED_OAUTH_HEADER + "' header set to obtain user identity via Freebase.");
    }
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    Map<String, String> params = (Map<String, String>) request.getParameterMap();
    for (Entry<String, String> e : params.entrySet()) {
        formparams.add(new BasicNameValuePair((String) e.getKey(), (String) e.getValue()));
    }
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
    HttpPost httpRequest = new HttpPost(USER_INFO_URL);
    httpRequest.setHeader(OAUTH_HEADER, oauth);
    httpRequest.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "OpenRefine Broker");
    httpRequest.setEntity(entity);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpRequest, responseHandler);
    JSONObject o = new JSONObject(responseBody);
    return o.getString("username");
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Aggregations

BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)39 HttpGet (org.apache.http.client.methods.HttpGet)26 Test (org.junit.Test)15 IOException (java.io.IOException)10 HttpPost (org.apache.http.client.methods.HttpPost)10 ArrayList (java.util.ArrayList)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 NameValuePair (org.apache.http.NameValuePair)6 HttpClient (org.apache.http.client.HttpClient)6 HttpResponseException (org.apache.http.client.HttpResponseException)6 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2