使用 Apache HttpClient 的 Java HTTPPost 请求 [英] Java HTTPPost Request with Apache HttpClient

查看:25
本文介绍了使用 Apache HttpClient 的 Java HTTPPost 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 java 程序来生成以下请求.我正在使用 Apache HttpClient 库,但仍然无法生成这样的请求:

I need a java program to generate the following request. I am using Apache HttpClient Library but still not able to produce a request like this:

这是我的python程序生成的,我写了一个等效的java程序.但它会抛出 403.

This is what my python program generates and i wrote an equivalent java program. but its throwing 403.

2012-09-10 15:12:05G 信息:G2OAuth 身份验证数据 = "3, 0.0.0.0, 0.0.0.0, 1347289925, 3223833979, crlakamai"2012-09-10 15:12:05G 信息:G2OAuth 签名字符串 = "3, 0.0.0.0, 0.0.0.0, 1347289925, 3223833979, akamai/182228\nx-akamai;version=acs;格式=xml\n"

2012-09-10 15:12:05G INFO: G2OAuth auth data = "3, 0.0.0.0, 0.0.0.0, 1347289925, 3223833979, crlakamai" 2012-09-10 15:12:05G INFO: G2OAuth sign string = "3, 0.0.0.0, 0.0.0.0, 1347289925, 3223833979, akamai/182228\nx-akamai-acs-action:version=1&action=dir&format=xml\n"

   send: 'POST /182228 HTTP/1.1\r\nHost: crl.api.akamailab.com\r\nAccept-Encoding: identity\r\nX-Akamai-ACS-Auth-Data: 3, 0.0.0.0, 0.0.0.0, 1347289925, 3223833979, crlsymc\r\nX-Akamai-ACS-Auth-Sign: eFnWtJBIyj4rxV3V0axF3w==\r\nX-Akamai-ACS-Action: version=1&action=dir&format=xml\r\n\r\n'

reply: 'HTTP/1.1 200 OK\r\n'
header: Server: Apache
header: Content-Type: text/html
header: Date: Mon, 10 Sep 2012 15:12:09 GMT
header: Content-Length: 31
header: Connection: keep-alive

响应如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<stat directory="/182232">
        <file type="file" name="log4j.properties" mtime="1346780907" size="301" md5="c92268157f1732a05c2027d151fc539a"/>
</stat>

这是我的 Java 代码:

Here is My Java Code:

    final HttpHost targetHost = new HttpHost("a.host.com", 80, "http");
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    final Credentials credentials = new UsernamePasswordCredentials("user","pass");
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), credentials);


    final HttpPost httpPostRequest = new HttpPost("akamai/182232");

    //Add your Data
    final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
    nameValuePairs.add(new BasicNameValuePair("Host: ", "a.host.com");
    nameValuePairs.add(new BasicNameValuePair("Accept-Encoding: ", "identity"));
    nameValuePairs.add(new BasicNameValuePair("Content-Length: ", "6"));

    httpPostRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    final HttpResponse response = httpClient.execute(targetHost, httpPostRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }

我的回复是这样的.

2012-09-10 11:31:22,600 DEBUG [wire] >> "POST /182228/a.crl HTTP/1.1[\r][\n]"
2012-09-10 11:31:22,601 DEBUG [wire] >> "Content-Length: 394[\r][\n]"
2012-09-10 11:31:22,601 DEBUG [wire] >> "Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1[\r][\n]"
2012-09-10 11:31:22,601 DEBUG [wire] >> "Host: crl.api.symclab.com:80[\r][\n]"
2012-09-10 11:31:22,601 DEBUG [wire] >> "Connection: Keep-Alive[\r][\n]"
2012-09-10 11:31:22,601 DEBUG [wire] >> "User-Agent: Apache-HttpClient/4.1.3 (java 1.5)[\r][\n]"
2012-09-10 11:31:22,602 DEBUG [wire] >> "[\r][\n]"

我想要接受编码另一个标题作为帖子的一部分,我该如何添加它们?我相信它必须是发布请求的一部分,而不是 http 标头.

I want Accept-Encoding another headers as part of post, how do i add them ? It hast to be part of the post request i believe and not http header.

推荐答案

Accept-Encoding 是 HTTP Header 的一部分,除了你误作为 POST 参数发送的参数:

Accept-Encoding is part of the HTTP Header, in addition to the parameters that you mistakenly sent as POST parameters:

这里是如何使用 HTTP 客户端发送它:

Here is how to send it using HTTP Client:

httpPostRequest.setHeader("Content-Length", "6"); 
httpPostRequest.setHeader("Accept-Encoding", "identity"); 
httpPostRequest.setHeader("Host", "a.host.com");

这篇关于使用 Apache HttpClient 的 Java HTTPPost 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆