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

标签:

/**

*java客户端发送http请求

*/

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("打印发送状态---->");

//System.out.println(method.getStatusCode());

int sendStatus = 0;

try {

sendStatus = httpClient.executeMethod(method);

System.out.println("打印发送状态");

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;

}

/*

发送form表单参数

*/

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", "沈阳"),

new NameValuePair("userID", ""),

new NameValuePair("theDate", "")};

post.setRequestBody(param);

post.releaseConnection();

return post;

}

/*

发送json数据

*/

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 服务端

func (this *WorkflowApiController) WorkFlowExtend() {

fmt.Println("打印post数据")

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("打印错误----->",err.Error())

}

fmt.Println(this.GetString("startCity"))

formdata := this.GetString("formdata")

fmt.Println("打印接收数据------>>>>>",formdata)

formdataMap := map[string]interface{}{}

json.Unmarshal([]byte(formdata), &formdataMap)

//fmt.Println("调用http接口打印--->>,",formdataMap)

resMap := map[string]interface{}{}

resMap["code"] = "1"

this.Data["json"] = resMap

this.ServeJson()

}

标签:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值