首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

远道httpclient 请求

2012-08-21 
远程httpclient 请求工具类 HttpClientUtil?public class HttpClientUtil {?? ??? public static String s

远程httpclient 请求

工具类 HttpClientUtil

?

public class HttpClientUtil {
??
??? public static String sendRequest(URL url) throws IOException {
??? ??? String result = "";
??? ??? try {
??? ??? ??? PostMethod method = new PostMethod(url.toString());
??? ??? ??? method.addRequestHeader("Cache-Control",
??? ??? ??? ??? ??? RequestConstants.REQUEST_CACHE_CONTROL);
??? ??? ??? method.addRequestHeader("SOAPAction",
??? ??? ??? ??? ??? RequestConstants.REQUEST_SOAP_ACTION);
??? ??? ??? method.addRequestHeader("Cookie", RequestConstants.REQUEST_COOKIE);
??? ??? ??? method.addRequestHeader("Host", url.getHost());
??? ??? ??? method.addRequestHeader("Accept", RequestConstants.REQUEST_ACCEPT);
??? ??? ??? method.setRequestEntity(null);
??? ??? ??? HttpClient httpClient = new HttpClient();
??? ??? ??? int status = httpClient.executeMethod(method);

??? ??? ??? if (method.getResponseContentLength() < 0) {
??? ??? ??? ??? ByteArrayOutputStream bout = new ByteArrayOutputStream();
??? ??? ??? ??? byte[] data = new byte[8192];
??? ??? ??? ??? int rsize = 0;
??? ??? ??? ??? int totalrsize = 0;
??? ??? ??? ??? InputStream in = method.getResponseBodyAsStream();
??? ??? ??? ??? do {
??? ??? ??? ??? ??? rsize = in.read(data);
??? ??? ??? ??? ??? if (rsize > 0) {
??? ??? ??? ??? ??? ??? totalrsize += rsize;
??? ??? ??? ??? ??? ??? bout.write(data, 0, rsize);
??? ??? ??? ??? ??? }
??? ??? ??? ??? } while (rsize > 0);
??? ??? ??? ??? result = new String(bout.toByteArray());
??? ??? ??? } else {
??? ??? ??? ??? byte data[] = method.getResponseBody();
??? ??? ??? ??? result = new String(data, "utf-8");
??? ??? ??? }
??? ??? ??? if (status != 200) {
??? ??? ??? ??? log.info("请求错误:[url:" + url.toString() + ",responseCode:"
??? ??? ??? ??? ??? ??? + status + "]");
??? ??? ??? ??? return null;
??? ??? ??? }
??? ??? } catch (Exception e) {
??? ??? ??? log.info("http请求异常!");
??? ??? }
??? ??? return result;
??? }

?

public interface RequestConstants {
?
??? public static final String REQUEST_CACHE_CONTROL = "no-cache";
??
??? public static final String REQUEST_SOAP_ACTION = """";
?
??? public static final String REQUEST_COOKIE = "JSESSIONID=A09JHGHKHU68624309UTY84932;";
?
??? public static final String REQUEST_ACCEPT = "application/soap+xml, application/dime, multipart/related, text/*";
???
}

?

action 类

??? public String listTicketsByCondition() throws IOException {
??? ??? String url = "http://192.168.3.205:8080/GenLotWBOS/queryTickInf.action";
??? ??? String messageid = this.getRequest().getParameter("messageid");
??? ??? String cpserial = this.getRequest().getParameter("cpserial");
??? ??? String tacketid = this.getRequest().getParameter("tacketid");
??? ??? PostHelper p = new PostHelper(url);
??? ??? if(StringUtils.hasLength(messageid)){
??? ??? ??? p.addParameter("messageid", messageid);
??? ??? }
??? ??? if(StringUtils.hasLength(cpserial)){
??? ??? ??? p.addParameter("cpserial", cpserial);
??? ??? }
??? ??? if(StringUtils.hasLength(tacketid)){
??? ??? ??? p.addParameter("orderno", tacketid);
??? ??? }
??? ??? String jsonMsg = p.post(url);
????
???
??? ??? this.getResponse().setCharacterEncoding("UTF-8");
??? ??? this.getResponse().getWriter().write(jsonMsg);
??? ??? this.getResponse().getWriter().flush();

??? ??? return null;
??? }

?

?

热点排行