HttpClient--BasicNameValuePair

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

import javax.xml.rpc.ServiceException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;

public class Test3 {
public static void main(String[] args) throws ServiceException, ClientProtocolException, IOException {
	HttpClient hc=HttpClients.createDefault();
	HttpPost post=new HttpPost("http://62.xx.xx.122:9xx0/TaxHttpService/tax_getInfo");
	Map map=new HashMap();
	map.put("infoKind",2);
	String params=JSONObject.fromObject(map).toString();
	
	List<BasicNameValuePair> param=new ArrayList<BasicNameValuePair>();
	param.add(new BasicNameValuePair("inputJson", params));
	//HttpEntity hh=new StringEntity(params,"UTF-8");
	UrlEncodedFormEntity he=new UrlEncodedFormEntity(param,"UTF-8");
	post.setEntity(he);
	HttpResponse res=hc.execute(post);
	HttpEntity entity=res.getEntity();
	String msg=EntityUtils.toString(entity,"UTF-8");
	System.out.println(msg);
}
}

传递简单{'a':{'c':'d'}}形式的json数据,只能用上面的转换来转换去才成功调通,

这里要记一下EntityUtils和UrlEncodedFormEntity类,一个转换HttpEntity为字符串,一个是对HttpEntity先转换成UTF-8,然后再用URLEncoder转码


下面构造的数据不能用Map存储键值再转换成JSON,暂时并不知道为什么,可能是对方接口解析接收参数的问题,所以弄得很古灵精怪的,用下面的方法才接通。

构造的数据是:

[inputJson={"infoKind":2,"cName":"fsdfs","taxRate":17,"invoicer":"zzzzz","sAddress":"ddddddddddd","sBank":"ddddddddddd","details":[{"amount":88.12,"goodsName":"asdaas","priceKind":0,"taxRate":17}]}]

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test4 {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		// TODO Auto-generated method stub
new Test4().test();
	}
public String invoice(){
	JSONObject obj=new JSONObject();
	obj.put("infoKind", 2);
	obj.put("cName", "fsdfs");
	obj.put("taxRate", 17);
	obj.put("invoicer", "zzzzz");
	obj.put("sAddress", "ddddddddddd");
	obj.put("sBank", "ddddddddddd");
	
	JSONArray arr=new JSONArray();
	JSONObject d=new JSONObject();
	d.put("amount", 88.12);
	d.put("goodsName", "asdaas");
	d.put("priceKind", 0);
	d.put("taxRate", 17);
 d.put("goodsTaxNo", "10101013301");
    d.put("taxPre", 0);
    d.put("taxPreCon", "免税");
    d.put("goodsNoVer", "1.0");
 
	//String jsonstr=d.toString();
	arr.add(d);
	obj.put("details", arr);
	System.out.println(obj.toString());
	return obj.toString();
}
public void test() throws ClientProtocolException, IOException{
	HttpClient hc=HttpClients.createDefault();
	HttpPost post=new HttpPost("http://11.11.11.11xxxx");
	List<BasicNameValuePair> nv=new ArrayList<BasicNameValuePair>();
	nv.add(new BasicNameValuePair("inputJson", invoice()));
	UrlEncodedFormEntity he=new UrlEncodedFormEntity(nv,"utf-8");
	post.setEntity(he);
	HttpResponse res=hc.execute(post);
	HttpEntity entity=res.getEntity();
	String msg=EntityUtils.toString(entity,"UTF-8");
	System.out.println(">>>>>>>>>>>>>>>>>"+msg);
}
}

总结:对接别人的接口时,切记要问清楚传递参数的格式,这里对方的参数格式是inputJson={'a':'c'}

  • 2
    点赞
  • 6
    收藏
  • 打赏
    打赏
  • 4
    评论
该资源含以下jar: commons-codec-1.6.jar commons-logging-1.1.1.jar fluent-hc-4.2.5.jar httpclient-4.2.5.jar httpclient-cache-4.2.5.jar httpcore-4.2.4.jar httpmime-4.2.5.jar import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HttpContext; antlr-2.7.7.jar aopalliance.jar asm-3.3.jar asm-commons-3.3.jar asm-tree-3.3.jar aspectjrt.jar aspectjweaver.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.apache.commons.logging-1.1.1.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang-2.5.jar commons-lang3-3.2.jar commons-logging-1.1.3.jar dom4j-1.6.1.jar druid-1.0.15.jar fastjson-1.1.37.jar freemarker-2.3.22.jar hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.2.0.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar jackson-annotations-2.5.0.jar jackson-core-2.5.0.jar jackson-databind-2.5.0.jar javassist-3.11.0.GA.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.0.Final.jar jedis-2.1.0.jar json.jar jsp-api-2.1.jar jstl.jar log4j-1.2.16.jar log4j-api-2.2.jar log4j-core-2.2.jar mybatis-3.3.0.jar mybatis-spring-1.2.3.jar mysql-connector-java-5.1.40-bin.jar og
该资源含以下jar: commons-codec-1.6.jar commons-logging-1.1.1.jar fluent-hc-4.2.5.jar httpclient-4.2.5.jar httpclient-cache-4.2.5.jar httpcore-4.2.4.jar httpmime-4.2.5.jar import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HttpContext; antlr-2.7.7.jar aopalliance.jar asm-3.3.jar asm-commons-3.3.jar asm-tree-3.3.jar aspectjrt.jar aspectjweaver.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.apache.commons.logging-1.1.1.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang-2.5.jar commons-lang3-3.2.jar commons-logging-1.1.3.jar dom4j-1.6.1.jar druid-1.0.15.jar fastjson-1.1.37.jar freemarker-2.3.22.jar hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.2.0.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar jackson-annotations-2.5.0.jar jackson-core-2.5.0.jar jackson-databind-2.5.0.jar javassist-3.11.0.GA.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.0.Final.jar jedis-2.1.0.jar json.jar jsp-api-2.1.jar jstl.jar log4j-1.2.16.jar log4j-api-2.2.jar log4j-core-2.2.jar mybatis-3.3.0.jar mybatis-spring-1.2.3.jar mysql-connector-java-5.1.40-bin.jar og

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 4

打赏作者

简乐君

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值