java http请求 apache,使用Apache HttpClient的Java HTTPPost请求

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:

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

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

The response looks like this:

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 nameValuePairs = new ArrayList(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());

}

My response looks like this.

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]"

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 is part of the HTTP Header, in addition to the parameters that you mistakenly sent as POST parameters:

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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值