/**
*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() }

  

java httpclient发送json 请求 ,go服务端接收的更多相关文章

  1. Httpclient发送json请求

    一.Httpclient发送json请求 public String RequestJsonPost(String url){    String strresponse = null;    try ...

  2. (九)springmvc之json的数据请求(客户端发送json数据到服务端)

    index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...

  3. Java httpClient 发送http请求

    RestTemplate ObjectMapper将string反序列化为WeatherResponse类 RestTemplate通过spring配置注入

  4. jquery发送json请求,给springmvc接收

    js var obj = { 'name':name, 'desc':desc, 'scheduleStartTime':scheduleStartTime, 'scheduleEndTime':sc ...

  5. python服务器端、客户端的模型,客服端发送请求,服务端进行响应(web.py)

    服务器端.客户端的模型,客服端发送的请求,服务端的响应 相当于启动了一个web server install web.py 接口框架用到的包 http://webpy.org/tutorial3.zh ...

  6. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  7. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    (一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...

  9. jq ajax传递json对象到服务端及contentType的用法

    目录 0.一般情况下,通过键值对的方式将参数传递到服务端 1.ajax 传递复杂json对象到服务端 2.content-Type 对asp.net mvc项目的重要性 0.一般情况下,通过键值对的方 ...

随机推荐

  1. Hash Length Extension Attacks

    catalogue . Hash函数的内部原理 . 漏洞原理 1. Hash函数的内部原理 0x1: 分组.Padding 哈希函数以区块为单位操作数据.比如说,MD5, SHA1, SHA256的区 ...

  2. 数据结构算法C语言实现(九)--- 拓展:由迷宫问题引申的AI贪吃蛇

    一.简述 [开发中]由于期末时间有限,而且要用到后面的最短路径(可能),所以打算小学期在实现这一部分

  3. InternalsVisibleToAttribute——把internal成员暴露给指定的友元程序集

    友元程序集简介 我们知道一个类中被定义为internal的成员(包括类型.方法.属性.变量.事件)是只能在同一个程序集中被访问到的(当然了,我这里说的是正常的方式,不包括通过反射来访问).这个规则在. ...

  4. python实现自动输入命令回车操作

    苦逼的在sf上等了一天(问题链接),都没人来解答,只好自己想办法,东平西凑还是勉强实现了,记录一下: 安装完python2.7后,在cmd命令行输入python回车,后出现python相关的提示信息, ...

  5. hihocoder #1327

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个只包含小写字母'a'-'z'的字符串 S ,你需要将 S 中的字符重新排序,使得任意两个相同的字符不连在一 ...

  6. HDU 5907 Find Q(简单字符串)

    传送门 Description Byteasar is addicted to the English letter 'q'. Now he comes across a string S consi ...

  7. HDU 1285 确定比赛名次(简单拓扑排序)

    题目链接: 传送门 确定比赛名次 Time Limit: 1000MS     Memory Limit: 65536K Description 有N个比赛队(1 Input 输入有若干组,每组中的第 ...

  8. FZU 2193 So Hard (有限小数转换最简分数)(想法题)

    题目链接: 传送门 So Hard Time Limit: 1000MS     Memory Limit: 65536K 题目描述 请将有限小数化为最简分数. 输入 一个整数n 表示需要转化的小数个 ...

  9. pyenv的使用

    开始想使用virtual实现不同的版本的py隔离,然后发现不太方便,然后发现了这货. pyenv安装(ubuntu环境 ➜ ~ git clone git://github.com/yyuu/pyen ...

  10. segmentfault.com mongo出识以及对数组的操作

    https://segmentfault.com/a/1190000003951602 首先推荐个工具,no-sql-manager-for-mongodb-professional,虽然收费,但是每 ...