Search in sources :

Example 36 with HttpPost

use of org.apache.http.client.methods.HttpPost in project nanohttpd by NanoHttpd.

the class TestNanoFileUpLoad method testPostWithMultipartFormUpload3.

@Test
public void testPostWithMultipartFormUpload3() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String textFileName = UPLOAD_JAVA_FILE;
    HttpPost post = new HttpPost("http://localhost:8192/uploadFile3");
    executeUpload(httpclient, textFileName, post);
    FileItem file = this.testServer.files.get("upfile").get(0);
    Assert.assertEquals(file.getSize(), new File(textFileName).length());
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) Test(org.junit.Test)

Example 37 with HttpPost

use of org.apache.http.client.methods.HttpPost in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostRequestWithMultipartExtremEncodedParameters.

@Test
public void testPostRequestWithMultipartExtremEncodedParameters() throws Exception {
    this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/chin");
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "sfsadfasdf", Charset.forName("UTF-8"));
    reqEntity.addPart("specialString", new StringBody("拖拉图片到浏览器,可以实现预览功能", "text/plain", Charset.forName("UTF-8")));
    reqEntity.addPart("gender", new StringBody("图片名称", Charset.forName("UTF-8")) {

        @Override
        public String getFilename() {
            return "图片名称";
        }
    });
    httppost.setEntity(reqEntity);
    HttpResponse response = this.httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    String responseBody = EntityUtils.toString(entity, "UTF-8");
    assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;gender=图片名称;specialString=拖拉图片到浏览器,可以实现预览功能", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 38 with HttpPost

use of org.apache.http.client.methods.HttpPost 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 39 with HttpPost

use of org.apache.http.client.methods.HttpPost 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 40 with HttpPost

use of org.apache.http.client.methods.HttpPost 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)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29