/**
*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. 洛谷P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  2. POJ3273 Monthly Expense

    查看原题 边界,就是边界和思维,怎么有效的判断中间值是大了还是小了,以及准确的找到边界!一个<写成<=就前功尽弃,还特别难找到错误! #include <cstdio> #in ...

  3. 数据结构算法C语言实现(十一)--- 3.4队列的链式表示和实现

    一.简介 FIFO. 二.头文件 //3_4_part1.h /** author:zhaoyu email:zhaoyu1995.com@gmail.com date:2016-6-9 note:r ...

  4. 解决Eclipse 项目报错:Unbound classpath container

    Eclipse出现下面两条报错: The project cannot be built until build path errors are resolved HelloWord Unknown ...

  5. Delicious Retouch 3

    今天发现一个photoshop的插件:Delicious Retouch 3,磨皮的,特好用,各种磨皮方法的合集.今后都不敢说自己会磨皮了. 插件的界面 插件的使用教程 链接:http://pan.b ...

  6. linux快速安装lamp环境

    我折腾过不少的lamp安装方式,lnmp也折腾过.不过因为windows下面apache用的比较多,各种配置也比较熟悉,因此最终还是选择了lamp这个架构. 由于是自己装虚拟机玩,所以各种安全措施都没 ...

  7. href和src的使用场景

    href和src的使用场景 href和src的用法虽然简单,但是有时候会突然记不起来该怎么用,且两者不可相互替换,下面列出来方便记忆,并给出具体区别. href的使用: 1.外部css引用:<l ...

  8. SaltStack配置管理之状态模块和jinja2(五)

    官方文档 https://docs.saltstack.com/en/latest/topics/states/index.html 配置管理之SLS Salt  State  SLS描述文件(YAM ...

  9. Redis+Spring缓存实例

    转自:小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).lis ...

  10. Kafka集群的安装和使用

    Kafka是一种高吞吐量的分布式发布订阅的消息队列系统,原本开发自LinkedIn,用作LinkedIn的活动流(ActivityStream)和运营数据处理管道(Pipeline)的基础.现在它已被 ...