应用场合,项目在做数据通信时,开始使用的是socket 做数据通信,最后搞了半天又说socket 通信对外不能提供服务,

需要做一个代理,然后就用到了HttpClient 。

 1.把请求数据使用httpClient 发送到某一个servlet.

 2.然后在servlet 使用socket 进行转发。达到目的。

代码如下: 最后一个参数是http client 请求url 报错时, 三次请求url

/** 把报文结构发送到服务器端 post */
public HttpMethod getPostMethod(String ip, String port, String result) {
String url = "http://" + HttpClientUtil.localIP() + ":"
+ HttpClientUtil.localPort() + "/httpclienttest/client";
PostMethod postMethod = new PostMethod(url);
postMethod.addParameter("ip", ip);
postMethod.addParameter("port", port);
postMethod.addParameter("result", result);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(RETRY_COUNT, SENT_RETRY));
return postMethod;
}




  HttpClient client=null; HttpMethod method = null; System.out.println("客户端正在连接..."); client = new HttpClient(); method = getPostMethod(HttpClientUtil.gxIP(),HttpClientUtil.gxPort(),resultAnt());// 使用POST方式提交数据 int state= client.executeMethod(method); System.out.println("state:"+state); String res = new String(method.getResponseBodyAsString().getBytes("GB2312"));//读取服务器端返回xml method.releaseConnection(); 

 

以下是服务器端 servlet 转发代码

@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String ip = req.getParameter("ip");String port = req.getParameter("port");String result = req.getParameter("result");System.out.println("IP:"+ip+"/tport:"+port+"/tresult:"+result);Socket socket = SocketUtil.getSocket(ip,Integer.valueOf(port));SocketUtil.send(socket,result);String respResult= SocketUtil.incept(socket);resp.getOutputStream().write(respResult.getBytes());socket.close();super.doPost(req, resp);} 

web.xml配置

<servlet> <servlet-name>client</servlet-name> <servlet-class>test.servlet.MyServlet</servlet-class></servlet><servlet-mapping> <servlet-name>client</servlet-name> <url-pattern>/client</url-pattern></servlet-mapping> 

SocketUtil.java

public class SocketUtil {private static Socket socket = null; /**获取一个socket 连接*/public static Socket getSocket(String ip,int port) {try {socket = new Socket(ip,port);} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return socket;}/** * 发送请求报文 * 注:不可以关闭流 否则会关闭对应的socket */public static void send(Socket socket, String msg) { PrintWriter out=null;try {out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);} catch (IOException e) {e.printStackTrace();} out.println(msg);}/** * 接收服务器返回的报文 * 注:不可以关闭流 否则会关闭对应的socket */public static String incept(Socket socket) {String result = null;try {BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));result = br.readLine();} catch (IOException e) {e.printStackTrace();} return result;}} 

SocketUtil.java 最终改成以下方式 在服务器发送过一的数据时,有固定的报文长度。

public class SocketUtil {
private static Socket socket = null;
/**获取一个socket 连接*/
public static Socket getSocket(String ip,int port) {
try {
socket = new Socket(ip,port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return socket;
}

/**
* 发送请求报文
* 注:不可以关闭流 否则会关闭对应的socket
*/
public static void send(Socket socket, byte[] msg) {
DataOutputStream out=null;
try {
out = new DataOutputStream((socket.getOutputStream()));
out.write(msg);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 接收服务器返回的报文
* 注:不可以关闭流 否则会关闭对应的socket
*/
public static byte [] incept(Socket socket) {
byte [] head = new byte[4];
byte [] body=null;
try {
BufferedInputStream bufIn = new BufferedInputStream(socket.getInputStream());
bufIn.read(head);
int len1 = Integer.parseInt(new String(head));
body = new byte[len1];
bufIn.read(body);
} catch (IOException e) {
e.printStackTrace();
}
return body;
}

}