第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Apache HTTP 客戶端將復(fù)雜參數(shù)傳遞給 POST 請(qǐng)求?

如何使用 Apache HTTP 客戶端將復(fù)雜參數(shù)傳遞給 POST 請(qǐng)求?

喵喵時(shí)光機(jī) 2024-01-17 16:35:56
我嘗試發(fā)送POST帶有這樣的正文的請(qǐng)求{  "method": "getAreas",  "methodProperties": {      "prop1" : "value1",      "prop2" : "value2",   }}這是我的代碼static final String HOST = "https://somehost.com";  public String sendPost(String method,      Map<String, String> methodProperties) throws ClientProtocolException, IOException {    HttpPost post = new HttpPost(HOST);    List<NameValuePair> urlParameters = new ArrayList<>();    urlParameters.add(new BasicNameValuePair("method", method));    List<NameValuePair> methodPropertiesList = methodProperties.entrySet().stream()                .map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))                .collect(Collectors.toList());    // ??? urlParameters.add(new BasicNameValuePair("methodProperties", methodPropertiesList));    post.setEntity(new UrlEncodedFormEntity(urlParameters));    try (CloseableHttpClient httpClient = HttpClients.createDefault();        CloseableHttpResponse response = httpClient.execute(post)) {      return EntityUtils.toString(response.getEntity());    }  }但 的構(gòu)造函數(shù)BasicNameValuePair適用(String, String)。所以我需要另一堂課。有什么辦法可以添加methodPropertiesList嗎urlParameters?
查看完整描述

3 回答

?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

您的請(qǐng)求看起來像 json 結(jié)構(gòu),因此發(fā)布如下數(shù)據(jù):


 class pojo1{

   String method;

   Map<String,String> methodProperties;

 }


String postUrl = "www.site.com";// put in your url

Gson gson = new Gson();

HttpClient httpClient = HttpClientBuilder.create().build();

HttpPost post = new HttpPost(postUrl);

StringEntity postingString = new StringEntity(gson.toJson(pojo1));//gson.tojson()    converts your pojo to json

post.setEntity(postingString);

post.setHeader("Content-type", "application/json");

HttpResponse  response = httpClient.execute(post);


查看完整回答
反對(duì) 回復(fù) 2024-01-17
?
慕無忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

對(duì)于這個(gè)問題有一個(gè)眾所周知的方法。在大多數(shù)情況下,您將創(chuàng)建自己的對(duì)象來描述要在 HttpPost 中發(fā)送的內(nèi)容。所以你會(huì)得到類似的東西:


static final String HOST = "https://somehost.com";


? public String sendPost(String method,

? ? ? Map<String, String> methodProperties) throws ClientProtocolException, IOException {


? ? HttpPost post = new HttpPost(HOST);

? ? MyResource resource = new MyResource();

? ? resource.setMethod(method);

? ? MyNestedResource nestedResource = new MyNestedResource();

? ? nestedResource.setMethodProperties(methodProperties);

? ? resource.setNestedResourceMethodProperties(nestedResource);


? ? StringEntity strEntity = new StringEntity(gson.toJson(resource));

? ? post.setEntity(strEntity);


? ? try (CloseableHttpClient httpClient = HttpClients.createDefault();

? ? ? ? CloseableHttpResponse response = httpClient.execute(post)) {


? ? ? return EntityUtils.toString(response.getEntity());

? ? }

? }

這通常是您使用嵌套結(jié)構(gòu)處理更復(fù)雜的 json 對(duì)象的方式。您必須為要發(fā)送的資源創(chuàng)建類(在您的示例中,它可能是一個(gè)類并在其中使用映射,但通常您也為嵌套對(duì)象創(chuàng)建一個(gè)類,如果它具有特定的結(jié)構(gòu))。


查看完整回答
反對(duì) 回復(fù) 2024-01-17
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊


static final String HOST = "https://somehost.com";


? public String sendPost(String method, Map<String, String> methodProperties) throws ClientProtocolException, IOException {


? ? HttpPost post = new HttpPost(HOST);??

? ? Gson gson = new Gson();


? ? Params params = new Params(method, methodProperties);

? ? StringEntity entity = new StringEntity(gson.toJson(params));? ?

? ? post.setEntity(entity);


? ? try (CloseableHttpClient httpClient = HttpClients.createDefault();

? ? ? ? CloseableHttpResponse response = httpClient.execute(post)) {


? ? ? return EntityUtils.toString(response.getEntity());

? ? }

? }

? class Params {


? ? String method;? ?

? ? Map<String, String> methodProperties;


? ? public Params(String method, Map<String, String> methodProperties) {

? ? ? this.method = method;

? ? ? this.methodProperties = methodProperties;

? ? }


? ? //getters

? }


查看完整回答
反對(duì) 回復(fù) 2024-01-17
  • 3 回答
  • 0 關(guān)注
  • 270 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)