String postUrl = "";//根据需要指定相应的url
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(postUrl);
NameValuePair[] pair = {
new NameValuePair("customerId",customerId),
new NameValuePair("reqStr",reqStr)
};
method.setRequestBody(pair);
client.getHttpConnectionManager().getParams().setConnectionTimeout(20000);
int statusCode = client.executeMethod(method);
if(statusCode != 200){
out.println("httpClient.executeMethod error. statusCode is "+statusCode);
return;
}
StringBuffer retStr = new StringBuffer();
InputStreamReader insr = new InputStreamReader(method.getResponseBodyAsStream());
int respInt = insr.read();
while(respInt != -1){
retStr.append(String.valueOf((char)respInt));
respInt = insr.read();
}
out.println(retStr.toString());
其中NameValuePair[]用于配置发起请求时需要的参数。程序需要使用到apache的HttpClient.jar包,读者可自行下载。