如何接收httpclient发送的请求
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何接收httpclient发送的请求相关的知识,希望对你有一定的参考价值。
参考技术A 客户端向服务器发送数据份两种情况SSL单向验证SSL双向验证1.SSL单向验证
代码:
Java代码
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ClientSendData
static Log log = LogFactory.getLog(ClientSendData.class);
private String Url;
// 初始化数据
public ClientSendData()
Url = "";
public String sendData(String data)
String receivedData = null;
try
Map paramsData = new HashMap();
paramsData.put("data", data);
receivedData = send(Url, paramsData);
catch (Exception e)
e.printStackTrace();
return receivedData;
public static String send(String url, Map paramsMap)
String result = null;
PostMethod postMethod = null;
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
postMethod = new PostMethod(url);
if (paramsMap != null && paramsMap.size() > 0)
NameValuePair[] datas = new NameValuePair[paramsMap.size()];
int index = 0;
for (String key : paramsMap.keySet())
datas[index++] = new NameValuePair(key, paramsMap.get(key));
postMethod.setRequestBody(datas);
HttpClientParams httparams = new HttpClientParams();
httparams.setSoTimeout(60000);
postMethod.setParams(httparams);
try
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_OK)
result = postMethod.getResponseBodyAsString();
log.info("发送功");
else
log.error(" http response status is " + statusCode);
catch (HttpException e)
log.error("error url=" + url, e);
catch (IOException e)
log.error("error url=" + url, e);
finally
if (postMethod != null)
postMethod.releaseConnection();
return result;
public static void main(String[] args)
ClientSendData t = new ClientSendData();
t.sendData("测试SSL单项连接向服务端发送数据!");
能现异
1.java.net.ConnectException: Connection refused: connect
服务器没启
2 .javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed
服务端证书信解决办见篇文章
3.java.net.SocketException: Software caused connection abort: recv failed
由于服务端配置SSL双向认证客户端发送数据按照服务器单向认证发送即没客户端证书信息起发送给服务端
4.org.apache.commons.httpclient.NoHttpResponseException
般服务端防火墙原拦截客户端请求
另外服务端负载重现问题
5.javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
由于服务端配置SSL双向认证客户端发送数据按照服务器单向认证发送即没客户端证书信息起发送给服务端服务端验证客户端证书发现客户端没证书断握手连接
2.SSL双向验证
双向验证暂知道何用HTTPCLIENT发送数据需要双向验证发送数据参考我另外文章另外知道HTTPCLIENT何双向验证发送数据恳请指教
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() }
以上是关于如何接收httpclient发送的请求的主要内容,如果未能解决你的问题,请参考以下文章
java httpclient发送json 请求 ,go服务端接收