/**
*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. 数据结构算法C语言实现(十四)--- 4.1&4.2串的类型定义、表示及实现

    一.简述 [暂无] 二.头文件 //4_2_part1.h /** author:zhaoyu */ //2016-6-10 //----串的定长顺序存储表示---- #include "h ...

  2. java循环遍历map

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class MapTest { pu ...

  3. CentOS 7.0 安装配置LAMP服务器方法(Apache+PHP+MariaDB)

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: #停止firewall服务 sys ...

  4. TypeScript Basic Types(基本类型)

    在学习TypeScript之前,我们需要先知道怎么才能让TypeScript写的东西正确的运行起来.有两种方式:使用Visual studio 和使用 NodeJs. 这里我选择的是NodeJs来编译 ...

  5. POJ 2796 Feel Good(单调栈)

    传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...

  6. python数据类型和字符串(三)

    一.变量 变量声明变量 #!/usr/bin/env python age= gender1='male' gender2='female' 变量作用:保存状态(程序的运行本质是一系列状态的变化,变量 ...

  7. 探究JavaScript中的五种事件处理程序

    探究JavaScript中的五种事件处理程序 我们知道JavaScript与HTML之间的交互是通过事件实现的.事件最早是在IE3和Netscape Navigator 2中出现的,当时是作为分担服务 ...

  8. SQL Server编程(02)自定义函数

    在编程过程中,我们通常把特定的功能语句块封装称函数,方便代码的重用.我们可以在SQL Server中自定义函数,根据函数返回值的区别,我们自定义的函数分两种:标量值函数和表值函数. 自定义函数的优点: ...

  9. js005-引用类型

    js005-引用类型 数据类型分为基本类型和引用类型:基本类型值如下:Undefined.Null.Bollean.Number.String 本章内容: 1.使用对象 2.创建并操作数组 3.理解基 ...

  10. windows7-SQLyog 安装图解

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