httpclient调用struts服务

package com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.http.NameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * 
 * 
 * @author muyunfei
 * 
 * <p>Modification History:</p> 
 * <p>Date       Author      Description</p>
 * <p>------------------------------------------------------------------</p>
 * <p>Oct 9, 2016           牟云飞       		 新建</p>
 */
public class BugMain {
	public static void main(String[] args) {
		try{
			 CloseableHttpClient httpclient = HttpClients.createDefault();
			 HttpPost httpPost= new HttpPost("http://www.XXXXXX.com/XXXXXX/serviceInvoke.do");
			 httpPost.setHeader("hy_serviceName", "chargeXXXXX");
			 httpPost.setHeader("account", "1556257XXXXX");
			 httpPost.setHeader("password ", "mima");
			 httpPost.setHeader("signature ", "dX5db5tnj1gQt0c7TkmB1A==");
			 httpPost.setHeader("timeStr ", "1499134848000");
             //------------
             //也可以用使用StringEntity,
             //但是需要增加
             //httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
             //UrlEncodedFormEntity 是StringEntity的封装
             //------------
			 List<NameValuePair> params = new ArrayList<NameValuePair>();
		     params.add(new BasicNameValuePair("channel", "0"));
		     params.add(new BasicNameValuePair("chargeId", "FS-Z-002"));//充电枪
		     params.add(new BasicNameValuePair("chargeNumber", "0"));//充满电
		     params.add(new BasicNameValuePair("chargeType", "4"));//充满电
		     params.add(new BasicNameValuePair("pointId", "0"));//充电枪号
		     params.add(new BasicNameValuePair("type", "0"));
		     httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			 // Create a custom response handler
			 ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() {
				//成功调用连接后,对返回数据进行的操作
	            public JSONObject handleResponse(
	                    final HttpResponse response) throws ClientProtocolException, IOException {
	                int status = response.getStatusLine().getStatusCode();
	                if (status >= 200 && status < 300) {
	                	//获得调用成功后  返回的数据
	                    HttpEntity entity = response.getEntity();
	                    if(null!=entity){
	                    	String result= EntityUtils.toString(entity);
	                        //根据字符串生成JSON对象
	               		 	JSONObject resultObj = JSONObject.fromObject(result);
	               		 	return resultObj;
	                    }else{
	                    	return null;
	                    }
	                } else {
	                    throw new ClientProtocolException("Unexpected response status: " + status);
	                }
	            }
	
	        };
	      //返回的json对象
	        JSONObject responseBody = httpclient.execute(httpPost, responseHandler);
	        System.out.println(responseBody);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牟云飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值