httpclient实例

  • h0_825296
    了解作者
  • 426.9KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-09 06:43
    上传日期
利用几个servlet之间通过httpclient传送数据流的实例
ServletPro.rar
内容介绍
package com.zlservlet; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; public class ServletDispatcher extends HttpServlet { /** * Constructor of the object. */ public ServletDispatcher() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String req_uri=request.getRequestURI(); System.out.println("-------req_url="+req_uri); String urlAddress="http://localhost:8088/ServletPro/servlet/ServletB"; //解析参数 String paramStr=""; Map paramMap=request.getParameterMap(); Iterator it=paramMap.keySet().iterator(); while(it.hasNext()){ String key=it.next().toString(); String param=key+"="+request.getParameter(key); paramStr=paramStr+param+"&"; } if(paramStr!=""){ paramStr="?"+paramStr; urlAddress=urlAddress+paramStr; } URL httpURL=null; PostMethod httpMethod = null; DataOutputStream out=null; try { httpURL = new URL(urlAddress); int port = httpURL.getPort(); if (port == -1) { port = httpURL.getDefaultPort(); } HttpClient httpclient = new HttpClient(); httpMethod = new PostMethod(urlAddress); httpMethod.setRequestBody(request.getInputStream()); int status=httpclient.executeMethod(httpMethod); //将服务端返回的数据流,返回给客户端 out = new DataOutputStream(response.getOutputStream()); out.write(httpMethod.getResponseBody()); response.setStatus(status); //System.out.println("-------sta="+status); } catch (Exception ex) { ex.printStackTrace(); } finally { httpMethod.releaseConnection(); out.close(); } //request.getRequestDispatcher(urlAddress).forward(request, response); //response.sendRedirect(urlAddress); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } public static String send(InputStream inputStream,String urlAddress) { //String urlAddress=GlobalKeys.getPolicePropertyByKey("InspectionServletUrl"); PostMethod httppost = null; try { URL httpURL = new URL(urlAddress); int port = httpURL.getPort(); if (port == -1) { port = httpURL.getDefaultPort(); } HttpClient httpclient = new HttpClient(); httppost = new PostMethod(urlAddress); httppost.setRequestBody(inputStream); int status=httpclient.executeMethod(httppost); InputStream result = httppost.getResponseBodyAsStream(); System.out.println("---statusCode="+status); //return buff; } catch (Exception ex) { ex.printStackTrace(); } finally { httppost.releaseConnection(); } return null; } }
评论
    相关推荐
    • HTTP SERVER
      簡單的 HTTP SERVER
    • http
      Miruken Http集成
    • http
      http
    • http sniffer
      采用LSP方式编写的http sniffer 包括 LSP的dll源码 服务安装程序源码 http sniffer源码
    • Http Analyzer
      HTTP通信分析工具 Capture Http packet and analyze it, very useful.
    • Http tunnel
      使用Http通道软件可以突破防火墙的限制,利用唯一Http访问的权限获得其他Internet应用。
    • http watch
      http watch .................
    • http redirect
      http redirect 模块,编译后 insmod /lib/modules/httpredirect.ko即可 基于2.6内核, 带有基于IP地址的数组管理功能, 简单修改,可以很方便跳转到自己需要的网页,并设置超时时间,已经调试通过,
    • http
      http
    • http
      http HTTP请求,响应HTTP中的CURD操作 握手具有HTTP路由服务器和使用nodejs的客户端,http模块套接字在套接字级别具有HTTP演示。