package com.wlq; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class RDRHttpCallSample { private static final int TIMEOUT = 50000; public final static String BAIDU_URL = "http://www.baidu.com" ; public final static String MSG_NULL = "content is null"; public static boolean validate(String source) { boolean rs = true; if (source.equals(MSG_NULL)) rs = false; if (source.indexOf("ERROR#") != -1 || source.indexOf("ErrorMsg") != -1 || source.indexOf("ERROR_TYPE") != -1) rs = false; return rs; } public static String getHttpUsefulInfo(StringBuffer buffer) { String rs = ""; if (buffer.indexOf("<body>") != -1) { int index = buffer.indexOf("<body>"); buffer.replace(0, index + 6, ""); index = buffer.indexOf("</body>"); buffer.replace(index, buffer.length(), ""); } rs = buffer.toString(); return rs.equals("") ? rs = MSG_NULL : rs; } public static StringBuffer doCall(String url, String queryStr){ StringBuffer rs = new StringBuffer(); HttpURLConnection conn = null; String sTotalString = ""; int respCode = 0; try{ URL console = new URL(url); conn = (HttpURLConnection) console.openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.getOutputStream().write(queryStr.getBytes()); conn.getOutputStream().flush(); conn.getOutputStream().close(); conn.connect(); respCode = conn.getResponseCode(); if(respCode == 200){ String sCurrentLine = ""; InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); while ((sCurrentLine = reader.readLine()) != null) if (sCurrentLine.length() > 0) sTotalString = sTotalString + sCurrentLine.trim(); }else{ sTotalString = "Call the rdr server error, error code is " + respCode; } }catch (Exception e) { e.printStackTrace(); }finally{ if(conn !=null) conn.disconnect(); if(validate(sTotalString)) rs.append(getHttpUsefulInfo(new StringBuffer(sTotalString))); } return rs; } public enum QueryStrGenerator{ BAIDU_CASE{ @Override public String getQueryStr(){ String str ="s?wd=what+if+you+go&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&inputT=2094"; return str; } }; abstract public String getQueryStr(); } public static void main(String...args){ String url = BAIDU_URL, queryStr = QueryStrGenerator.BAIDU_CASE.getQueryStr(); StringBuffer rs = doCall(url, queryStr); System.out.println(rs.toString()); } }
详细解决方案
Sample-simulate the http request
热度:834 发布时间:2012-09-09 09:27:54.0
相关解决方案
- request.setAttribute("list" "''");该怎么解决
- struts HTTP Status 500解决办法
- request.setAttribute("message" message)不能传int参数,该怎么处理
- RequestDispatcher rd = request.getRequestDispatcher跳转页面出现有关问题
- request.setAttribute("message" message)不能传int参数,该怎么解决
- 为何jsp request.getParameter取不到值,但request.getQueryString()却有值呢
- httpclient老是提示java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpCon
- 求老妪能解解释Response、Request
- 启动tomcat后,http://localhost:8080无法打开的有关问题
- 相干tomcat HTTP Status 500的疑问,麻烦大家进来看看
- Web Service 错误:服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/
- request.setAttribute()有关问题
- @Context HttpServletRequest request,@Context HttpServletResponse response,该如何解决
- HTTP Status 404 - /jspdemo/Register.jsp,该怎么解决
- 'http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/,该如何处理
- org.apache.http.client.HttpClient与org.apache.commons.httpclient.HttpClient的差别
- HTTP 404有关问题
- List items = upload.parseRequest(request);替空kindeditor SpringMvc
- myeclipse<%@ taglib prefix="c" uri="http://java.sun.com/jst1/core_rt"%>解决思路
- ,http 200状态码
- tomcat新手有关问题 HTTP Status 404 - /manager/
- java http 分段上载
- 急紧急十分紧急JSP,「focus="<%= JspUtil.getFirstProperty(request, null) %>」哪位高手给解释上这句话的作用?一有答案立即结帖
- struts2<s:if test=${pb.pageTotal>0}>出现有关问题 如果小弟我这样写是正确的<s:if test=#request.pb.pageT
- soap调用webservice,报错:Root element of a SOAP message must be: 'http://schemas.xmls,该怎么处理
- HTTP Status 404如何解决
- org.apache.tomcat.util.http.Parameters processParameters,该怎么解决
- 如何找到http 404异常发生时的 Request URL
- request.getSession报java.lang.NullPointerException,该如何解决
- 新手,HTTP Status 404 有关问题