/**
*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. python 生成器和递归

    生成器 1.定义 问题:python会把对象放到内存中,我们每次定义变量.列表等都会在内存中占用对应的地址块,所以当内存容量一定时,列表的容量会受到内存的限制,而且假如我们创建了一个包含200万个元素 ...

  2. ubuntu nginx 安装以及配置文件详解

    1.到nginx官网下载源码包.最好下载稳定版本,nginx官网http://www.nginx.org/ 2.安装nginx依赖包运行命令: sudo apt-get install libssl- ...

  3. 从Paxos到ZooKeeper-四、ZooKeeper技术内幕

    本文将从系统模型.序列化与协议.客户端工作原理.会话.服务端工作原理以及数据存储等方面来揭示ZooKeeper的技术内幕. 一.系统模型 1.1 数据模型 ZooKeeper的视图结构使用了其特有的& ...

  4. SQL ALTER TABLE 语句在项目中的使用

    1.在实际的项目开发过程中,之前已经创建好的实体类可能需要增加/删除字段,亦或是更改已有字段的属性,比如主键的增长策略从自增型改为UUID型,那么就会涉及到 SQL 中 alter table 语句的 ...

  5. winndows7、office2013 激活信息还原

    windows7激活信息还原 1.现在“计算机”,右键中的“管理”中的“服务”中禁止Software Protection 2.还原路径C:\Windows\ServiceProfiles\Netwo ...

  6. 在Nginx中部署基于IP的虚拟主机

    一.虚拟主机概念 虚拟主机是在网络服务器上划分出一定的磁盘空间供用户放置站点.应用组件等,提供必要的站点功能.数据存放和传输功能.所谓虚拟主机,也叫"网站空间", 就是把一台运行在 ...

  7. windows7-SQLyog 安装图解

    双击: 双击已下载的SQLyog Enterprise 安装文件,点击“next”,选择“I accept...”,勾选安装组件,选择安装目录,等待安装完成. 协议:选择我接受 选择操作   选择路径 ...

  8. JVM内存简单理解

    1.首先简单说一下CPU与内存之间的关系 CPU运转速度快,磁盘的读写速度远远不及CPU运转速度,所以设计了内存来缓冲CPU等待磁盘读写:随着CPU的发展,内存读写也远远跟不上CPU的读写速度,CPU ...

  9. docker快速启动脚本

    #!/bin/sh PID=$(docker inspect --format "{{.State.Pid}}" $1) nsenter -t $PID -u -i -n -p n ...

  10. Unity3D SceneView Camera

    http://forum.unity3d.com/threads/moving-scene-view-camera-from-editor-script.64920/