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()
}
java httpclient发送json 请求 ,go服务端接收的更多相关文章
- Httpclient发送json请求
一.Httpclient发送json请求 public String RequestJsonPost(String url){ String strresponse = null; try ...
- (九)springmvc之json的数据请求(客户端发送json数据到服务端)
index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- Java httpClient 发送http请求
RestTemplate ObjectMapper将string反序列化为WeatherResponse类 RestTemplate通过spring配置注入
- jquery发送json请求,给springmvc接收
js var obj = { 'name':name, 'desc':desc, 'scheduleStartTime':scheduleStartTime, 'scheduleEndTime':sc ...
- python服务器端、客户端的模型,客服端发送请求,服务端进行响应(web.py)
服务器端.客户端的模型,客服端发送的请求,服务端的响应 相当于启动了一个web server install web.py 接口框架用到的包 http://webpy.org/tutorial3.zh ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- jq ajax传递json对象到服务端及contentType的用法
目录 0.一般情况下,通过键值对的方式将参数传递到服务端 1.ajax 传递复杂json对象到服务端 2.content-Type 对asp.net mvc项目的重要性 0.一般情况下,通过键值对的方 ...
随机推荐
- 分析ffmpeg解析ts流信息的源码
花费一些时间,然后全部扔了.为了不忘记和抛砖引玉,特发此贴. ffmpeg解析ts流 1.目的 打算软件方式解析出pat,pmt等码流信息 2.源代码所在位置 下载ffmpeg ...
- 批处理(.bat)For命令使用
for命令使用 @echo off REM 打印当前目录里所有子目录的名称 REM for /d %%i in (*) do @echo %%i REM 打印当前目录里所有名字为3个字符的子目录的名称 ...
- MOOCULUS微积分-2: 数列与级数学习笔记 1. Sequences
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- iOS - 沙盒中,如何判断存在文件、目录
在iOS开发中,在沙盒中创建沙盒一些存储各个功能的文件目录或者文件. 使用: [NSFileManager defaultManager] 1.判断目录,用她可以. 2.判断文件,用她可以. 3.创建 ...
- mainBundle和CustomBundle
iOS中NSBundle类 An NSBundle object represents a location in the file system that groups code and resou ...
- 修改nginx的访问目录以及遇到的403错误修改总结
对于这个问题困扰了我好几天,前篇文章介绍了图片服务器的使用,但是两个服务器如何进行通话访问呢,即如何通过nginx来访问ftp服务器上的资源文件呢,这里面需要修改nginx的配置文件(vi /usr/ ...
- HTML <!DOCTYPE> 标签 布局引用的几种方法 行级元素与块级元素
HTML <!DOCTYPE> 标签 <!DOCTYPE html> <html> <head> <title>文档的标题</titl ...
- 生成N个二进制位的组合
#include "stdafx.h" #include "stdlib.h" #include "stdio.h" #include &l ...
- linq lamda
var query6 = CustomerList.SelectMany(c => c.Orders);var query6= from c in CustomerList ...
- Eclipse搭建Python开发环境+Python中文处理
1.基本需求 1.Eclipse 集成开发环境下载 http://115.com/file/c2vz7io5 JDK6下载 http://115.com/file/c2vz7idq 2. ...