HttpClient NameValuePair HttpPost JSONObject 发送请求数据

该篇博客介绍了如何使用Apache HttpClient库通过HttpPost方法发送带有NameValuePair和JSONObject的HTTP请求。示例代码展示了如何构造请求参数,包括创建NameValuePair、使用UrlEncodedFormEntity编码参数,并通过DefaultHttpClient执行POST请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import net.sf.json.JSONObject; 
import java.util.ArrayList;  
import java.util.List;    
import org.apache.http.NameValuePair;  
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.DefaultHttpClient;  
import org.apache.http.message.BasicNameValuePair;  
  
public class HttpTests {  
  
    /** 
     * @param args 
     * @throws Exception 
     */  
    public static void main(String[] args) throws Exception {
        String param1="zhongup";
        HttpClient httpclient = new DefaultHttpClient();  
        HttpPost httpPost = new HttpPost("******/abc");  
        List<NameValuePair> formParams = new ArrayList<NameValuePair>();     
        formParams.add(new BasicNameValuePair("param1", param1));     
        formParams.add(new BasicNameValuePair("param2", "value2"));            
        HttpEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");     
        httpPost.setEntity(entity);  
        httpclient.execute(httpPost);  
        httpclient.getConnectionManager().shutdown();  
    }  
  
}  









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'}


















下面构造的数据不能用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}]}]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值