日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java httpclient发送json 请求 ,go服务端接收

發(fā)布時(shí)間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java httpclient发送json 请求 ,go服务端接收 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/**
*java客戶端發(fā)送http請(qǐng)求
*/
package com.xx.httptest;/*** Created by yq on 16/6/27.*/import java.io.IOException; import java.net.URLEncoder; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.http.params.CoreConnectionPNames; import org.json.JSONException; import org.json.JSONObject;public class HttpClientTest {public static void main(String[] args) throws Exception {String url = "http://localhost:8030/workflowapi/workflowextend";String host = "www.127.0.0.1";String param = "startCity=" + URLEncoder.encode("杭州", "utf-8") + "&lastCity=&theDate=&userID=";HttpClient httpClient = new HttpClient();httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);//httpClient.getHostConfiguration().setHost(host, 8030, "http");//HttpMethod method = getMethod(url, param);HttpMethod method = postMethod1(url);System.out.println("打印發(fā)送狀態(tài)---->");//System.out.println(method.getStatusCode());int sendStatus = 0;try {sendStatus = httpClient.executeMethod(method);System.out.println("打印發(fā)送狀態(tài)");System.out.println(sendStatus);System.out.println("dddddddd");String response = method.getResponseBodyAsString();System.out.println(response);} catch (Exception e) {e.printStackTrace();} finally {method.releaseConnection();}//String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));???????????????//System.out.println(response);}private static HttpMethod getMethod(String url,String param) throws IOException {GetMethod get = new GetMethod(url + "?" + param);get.releaseConnection();return get;}/*發(fā)送form表單參數(shù)*/private static HttpMethod postMethod(String url) throws IOException {PostMethod post = new PostMethod(url);post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");NameValuePair[] param = {new NameValuePair("startCity", "杭州"),new NameValuePair("lastCity", "沈陽(yáng)"),new NameValuePair("userID", ""),new NameValuePair("theDate", "")};post.setRequestBody(param);post.releaseConnection();return post;}/*發(fā)送json數(shù)據(jù)*/private static HttpMethod postMethod1(String url) throws IOException{PostMethod post = new PostMethod(url);//post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");post.setRequestHeader("Content-Type","application/json");JSONObject jsonObject = new JSONObject();JSONObject jsonObject2 = new JSONObject();try {jsonObject.put("aaaaa","ddddd");jsonObject.put("bbbbb","ddddd2");jsonObject.put("ccccc","ddddd3");jsonObject2.put("55555",jsonObject);jsonObject2.put("66666","testtest");} catch (JSONException e) {e.printStackTrace();}RequestEntity requestEntity = new StringRequestEntity(jsonObject2.toString(),"text/xml","UTF-8");post.setRequestEntity(requestEntity);post.releaseConnection();return post;}}

  

go 服務(wù)端

func (this *WorkflowApiController) WorkFlowExtend() {fmt.Println("打印post數(shù)據(jù)")fmt.Println(this.Ctx.Request.Body)body, _ := ioutil.ReadAll(this.Ctx.Request.Body)var dat map[string]interface{}if err := json.Unmarshal(body, &dat); err == nil {fmt.Println("打印map----->",dat)}else {fmt.Println("打印錯(cuò)誤----->",err.Error())}fmt.Println(this.GetString("startCity"))formdata := this.GetString("formdata")fmt.Println("打印接收數(shù)據(jù)------>>>>>",formdata)formdataMap := map[string]interface{}{}json.Unmarshal([]byte(formdata), &formdataMap)//fmt.Println("調(diào)用http接口打印--->>,",formdataMap)resMap := map[string]interface{}{}resMap["code"] = "1"this.Data["json"] = resMapthis.ServeJson()}

  

轉(zhuǎn)載于:https://www.cnblogs.com/8899man/p/5622373.html

總結(jié)

以上是生活随笔為你收集整理的java httpclient发送json 请求 ,go服务端接收的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。