Search in sources :

Example 96 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.

the class PostResponseCreatorTest method testCustomPostResponseCreator.

public void testCustomPostResponseCreator() throws Exception {
    final PostMethod post = new PostMethod(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX);
    post.addParameter(":responseType", "custom");
    post.setFollowRedirects(false);
    final int status = httpClient.executeMethod(post);
    assertEquals("Unexpected status response", 201, status);
    assertEquals("Thanks!", post.getResponseBodyAsString());
    post.releaseConnection();
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 97 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project engine by craftercms.

the class HttpProxyImpl method createPostMethod.

protected HttpMethod createPostMethod(String url, HttpServletRequest request) throws IOException {
    PostMethod postMethod = new PostMethod(url);
    copyRequestHeadersToMethod(postMethod, request);
    copyRequestBodyToMethod(postMethod, request);
    return postMethod;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 98 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project dianping-open-sdk by dianping.

the class ApiTool method requestPostApi.

public static String requestPostApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    PostMethod method = new PostMethod(apiUrl);
    try {
        String sign = sign(appKey, secret, paramMap);
        paramMap.put("sign", sign);
        paramMap.put("appkey", appKey);
        // 设置HTTP Post数据
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            method.addParameter(entry.getKey(), entry.getValue());
        }
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Map(java.util.Map)

Example 99 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project dianping-open-sdk by dianping.

the class DemoApiTool method requestPostApi.

public static String requestPostApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    PostMethod method = new PostMethod(apiUrl);
    try {
        String sign = sign(appKey, secret, paramMap);
        paramMap.put("sign", sign);
        paramMap.put("appkey", appKey);
        // 设置HTTP Post数据
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            method.addParameter(entry.getKey(), entry.getValue());
        }
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Map(java.util.Map)

Example 100 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project camel by apache.

the class UndertowMethodRestricTest method testProperHttpMethod.

@Test
public void testProperHttpMethod() throws Exception {
    HttpClient httpClient = new HttpClient();
    PostMethod httpPost = new PostMethod(url);
    StringRequestEntity reqEntity = new StringRequestEntity("This is a test", null, null);
    httpPost.setRequestEntity(reqEntity);
    int status = httpClient.executeMethod(httpPost);
    assertEquals(200, status);
    String result = httpPost.getResponseBodyAsString();
    assertEquals("This is a test response", result);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) Test(org.junit.Test)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11