HttpClient NameValuePair HttpPost JSONObject send request data

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);  
}  
}  
pass simple {' The json data in the form of a':{'c':'d'}} can only be successfully adjusted by using the above conversion.






Remember the EntityUtils and UrlEncodedFormEntity classes, one converts HttpEntity to a string, and the other is Convert HttpEntity to UTF-8 first, and then use URLEncoder to transcode. The data constructed
below cannot use Map to store key values ​​and then convert them into JSON. I don't know why for the time being. It may be the problem of the other party's interface parsing and receiving parameters, so it is very confusing. Weird, use the following method to connect.


The constructed data is:


[inputJson={"infoKind":2,"cName":"fsdfs","taxRate":17,"invoicer":"zzzzz","sAddress":"dddddddddddd","sBank":" ddddddddddd","details":[{"amount":88.12,"goodsName":




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);  
}  
}  
Summary: When connecting to other people's interfaces, remember to ask clearly The format of passing parameters, here the parameter format of the other party is inputJson={'a':'c'}


















. The data constructed below cannot be stored in a Map and then converted into JSON. I don't know why, it may be that the other party's interface parses the received parameters. The problem, so it is very strange, and the following method is used to connect.


The constructed data is:


[inputJson={"infoKind":2,"cName":"fsdfs","taxRate":17,"invoicer":"zzzzz","sAddress":"dddddddddddd","

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951724&siteId=291194637