亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

java HttpClient傳輸json格式的參數(shù)實(shí)例講解

瀏覽:5日期:2022-08-17 18:57:41

最近的一個(gè)接口項(xiàng)目,傳的參數(shù)要求是json,需要特殊處理一下。

重點(diǎn)是這兩句話:

httpPost.setHeader('Content-Type', 'application/json;charset=UTF-8');se.setContentType(CONTENT_TYPE_TEXT_JSON);

這兩句話的作用與jmeter的設(shè)置header信息類似

package com.base;import java.io.UnsupportedEncodingException;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.conn.PoolingClientConnectionManager;import org.apache.http.util.EntityUtils;/** * @author QiaoJiafei * @version 創(chuàng)建時(shí)間:2015年11月4日 下午1:55:45 * 類說明 */public class HttpGetByJson { public static void main(String args[]) throws Exception{ final String CONTENT_TYPE_TEXT_JSON = 'text/json'; DefaultHttpClient client = new DefaultHttpClient( new PoolingClientConnectionManager()); String url = 'http://172.16.30.226:8091/svc/authentication/register'; String js = '{'userName':'18600363833','validateChar':'706923','randomChar':'706923','password':'123456','confirmPwd':'123456','recommendMobile':'','idCard':'320601197608285792','realName':'闕巖','verifyCode'}'; HttpPost httpPost = new HttpPost(url); httpPost.setHeader('Content-Type', 'application/json;charset=UTF-8'); StringEntity se = new StringEntity(js); se.setContentType(CONTENT_TYPE_TEXT_JSON); httpPost.setEntity(se); CloseableHttpResponse response2 = null; response2 = client.execute(httpPost); HttpEntity entity2 = null; entity2 = response2.getEntity(); String s2 = EntityUtils.toString(entity2, 'UTF-8'); System.out.println(s2); } }

補(bǔ)充:HttpClient以json形式的參數(shù)調(diào)用http接口并對返回的json數(shù)據(jù)進(jìn)行處理(可以帶文件)

1、參數(shù)的url就是被調(diào)用的地址,map是你要傳的參數(shù)。參數(shù)轉(zhuǎn)成json我使用的是gson方式轉(zhuǎn)換的。

主要使用的jar包有httpclient-4.5.3.jar、httpcore-4.4.6.jar、commons-codec-1.9.jar、gson-2.2.4.jar和commons-logging-1.2.jar。

如果發(fā)送的post請求想傳送文件,需添加httpmime-4.5.3.jar包,并設(shè)置如下代碼:

HttpEntity multipartEntityBuilder = MultipartEntityBuilder.create().addBinaryBody('file', new File('D:workspaceprogrammWebContentprogramm1991.zip')).build();

第一個(gè)參數(shù)表示請求字段名,第二個(gè)參數(shù)就是文件。

還想添加參數(shù)則

HttpEntity multipartEntityBuilder = MultipartEntityBuilder.create().addTextBody('name', '張三').addBinaryBody('file', new File('D:workspaceprogrammWebContentprogramm1991.zip')).build();httpPost.setEntity(multipartEntityBuilder);

import java.io.IOException;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import com.google.gson.Gson;public class HttpClientUtil { private final static String CONTENT_TYPE_TEXT_JSON = 'text/json'; public static String postRequest(String url, Map<String, Object> param) throws ClientProtocolException, IOException{ CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader('Content-Type', 'application/json;charset=UTF-8'); Gson gson = new Gson(); String parameter = gson.toJson(param); StringEntity se = new StringEntity(parameter); se.setContentType(CONTENT_TYPE_TEXT_JSON); httpPost.setEntity(se); CloseableHttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, 'UTF-8'); return result; }}

2、返回的結(jié)果也可以使用gson轉(zhuǎn)換成對象進(jìn)行下一步操作。

import com.google.gson.Gson;public class GsonUtil { public static <T> T jsonToObject(String jsonData, Class<T> type) { Gson gson = new Gson(); T result = gson.fromJson(jsonData, type); return result; } public static void main(String[] args) { String json = '{’id’:’1’,’name’:’zhang’,’address’:’Hubei’}'; jsonToObject(json, Person.class); Person person = jsonToObject(json, Person.class); System.out.println(person); }}

建立要轉(zhuǎn)成的對象的類。

import java.util.Date;public class Person { private int id; private String name; private int age; private String address;public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return 'Person [id=' + id + ', name=' + name + ', age=' + age + ', address=' + address + ']'; }}

3、發(fā)送以鍵值對形式的參數(shù)的post請求

package com.avatarmind.httpclient;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;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.util.EntityUtils;public class HttpClient3 { public static void main(String[] args) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); String url = 'http://yuntuapi.amap.com/datamanage/table/create'; HttpPost httpPost = new HttpPost(url); // 參數(shù)形式為key=value&key=value List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair('key', '060212638b94290e3dd0648c15753b64')); formparams.add(new BasicNameValuePair('name', '火狐')); // 加utf-8進(jìn)行編碼 UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, 'UTF-8'); httpPost.setEntity(uefEntity); CloseableHttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, 'UTF-8'); System.out.println(result); }}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 久久在现 | 日本人一级毛片视频 | 女人被狂躁的视频免费免费看 | 成在线人免费视频一区二区三区 | 51精品视频在线观看视频 | 97视频在线免费观看 | 91精品国产综合久久久久久 | 2022国产精品自拍 | 尤物视频在线观看免费 | 久久一级黄色片 | 久久视频精品线视频在线网站 | 日韩一级生活片 | 亚洲一区 中文字幕 久久 | 亚洲国产成人资源在线桃色 | 免费在线你懂的 | 国产高清专区 | 成年网站视频在线观看 | 国产中文字幕久久 | 日本成人二区 | 亚洲综合91 | 欧美一区二区三区在线观看不卡 | 一a级毛片| 欧美成人午夜不卡在线视频 | 国产精品女人在线观看 | 精品在线免费视频 | 91福利网址| 免费看欧美一级a毛片 | 亚洲黄网址 | 91正在播放极品白嫩在线观看 | 一级毛片免费毛片一级毛片免费 | 久久视频国产 | 欧美肥老妇做爰视频 | 麻豆传媒入口直接进入免费版 | 国产亚洲欧美在线观看的 | 久久久久久免费播放一级毛片 | 午夜精品久久久久久久爽 | 免费久久一级欧美特大黄 | 欧美三级一级 | 看一级毛片免费观看视频 | 二级特黄绝大片免费视频大片 | 国产精品一区二区三区四区五区 |