在项目中,经常会进行垮项目通讯,最常用的就是webservice接口,但是有时候在内部平台和平台间通讯,我们也会简单的使用springMVC 控制器暴露出接口地址,然后另一个项目发送http请求进行报文通讯等操作
首先写一个简单的MVC控制器
package com.lovo.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.test.Log4jTest;
@Controller
public class httpClientServer {
Logger log=Logger.getLogger(httpClientServer.class);
@RequestMapping(value="/htteTestServlet.do",method=RequestMethod.POST)
public void doPostServer(HttpServletRequest request, HttpServletResponse response){
try {
//设置编码集
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
//方法1 ,从消息体中取值
//获取消息体内容
BufferedReader reader = request.getReader();
StringBuffer buffer = new StringBuffer();
String temp = null;
while((temp = reader.readLine()) != null){
System.out.println(temp);
buffer.append(temp);
}
//进行URL解码
String rspTtr = URLDecoder.decode(buffer.toString(),"utf-8");
//返回处理后的消息体
response.getOutputStream().write(rspTtr.getBytes("UTF-8"));
//方法2:因为封装到body,也可以直接用gerparameter取值
//对请求的报文进行处理
/* String xml = request.getParameter("xml");
System.out.println("服务端"+xml);
//响应客户端,返回报文
response.getOutputStream().write(xml.getBytes("UTF-8"));*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
写一个简单的httpclient发送post请求
package com.lovo.utils;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientUtils {
public static String postHttp(String url,String body) {
String mesg = null;
//构建HttpClient实例
HttpClient httpClient = new HttpClient();
//设置请求超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
//设置响应超时时间
httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);
//构造PostMethod的实例
PostMethod postMethod=new PostMethod(url);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
//封装消息体内容
NameValuePair[] data = {new NameValuePair("xml",body)};
postMethod.setRequestBody(data);
try {
//执行post请求
httpClient.executeMethod(postMethod);
//可以对响应回来的报文进行处理
mesg = postMethod.getResponseBodyAsString();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭连接释放资源的方法
postMethod.releaseConnection();
//((SimpleHttpConnectionManager)httpClient.getHttpConnectionManager()).shutdown();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
return mesg;
}
public static void main(String[] args) {
String xml = "<?xml version='1.0' encoding='utf-8'?>"
+ "<shopping><product code='1'>"
+ "<name>老坛酸菜</name><"
+ "price>3.5</price>"
+ "<factory>统一</factory>"
+ "</product><product code='2'>"
+ "<name>加多宝</name>"
+ "<price>6</price>"
+ "<factory>加多宝</factory>"
+ "</product>"
+ "</shopping>";
System.out.println(xml);
String a = postHttp("http://localhost:8080/Spring/htteTestServlet.do", xml);
System.out.println("客户端"+a);
}
}
这样就完成了一个简单的通讯