httpclient post

来源:互联网 发布:编程语言选择 编辑:程序博客网 时间:2024/06/10 08:37
package com.cstc.interfaces.hainan;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;


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;


import com.cstc.common.JSONCommon;
import com.cstc.common.entity.IntefaceAddress;


public class TaxConsultation extends HttpServlet {



public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}



@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String imgCode = request.getParameter("randcode");
String checkCode = (String) request.getSession().getAttribute("checkCode");
if(imgCode.equalsIgnoreCase(checkCode)){
 JSONCommon  json = new JSONCommon();
  String jsons=null;
  IntefaceAddress ads=IntefaceAddress.getIntefaceAddress();
  String url = ads.getUrl();
  url +="/nszx/submitMobileOnlineQuestion.jhtml"; 
  System.out.println(url);
  Enumeration<String> map=request.getParameterNames();
  HttpClient client=new HttpClient();                           //三实例化  httpclient
   client.getHttpConnectionManager().getParams().setSoTimeout(60000); 
   client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");  
   PostMethod method = new PostMethod(url);                       //实例化 postmethod 带url的
   method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
   while(map.hasMoreElements()){
    String key=map.nextElement();
    method.addParameter(key,request.getParameter(key));       //
  }
   try{
     client.executeMethod(method);
     jsons=new String(method.getResponseBodyAsString().getBytes("gb2312"));
    }
      catch(Exception e){
      e.printStackTrace();
      }finally{
     method.releaseConnection();
      }
      json.writeJSON(response, jsons);
}
}
}
0 0