日期:2014-05-20 浏览次数:21175 次
url="http://www.dianpin.com"; CityDetails citys = new CityDetails(); // String targetRequest=citys.getCityDetails(); String targetRequest = "aaa"; HttpClient httpClient = new HttpClient(); httpClient.getHostConfiguration().setHost(url); // 创建POST方法的实例 PostMethod postMethod = new PostMethod(); // 使用系统提供的默认的恢复策略 postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); postMethod.setRequestHeader( "User-Agent", "fake"); // 填入各个表单域的值 NameValuePair[] data = { new NameValuePair("request", targetRequest) }; // 将表单的值放入postMethod中 postMethod.setRequestBody(data); try { // 执行postMethod int statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发 // 301或者302 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // 从头中取出转向的地址 Header locationHeader = postMethod .getResponseHeader("location"); String location = null; if (locationHeader != null) { location = locationHeader.getValue(); System.out .println("The page was redirected to:" + location); } else { System.err.println("Location field value is null."); } } if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + postMethod.getStatusLine()); } // 读取内容 byte[] responseBody = postMethod.getResponseBody(); // 处理内容 String str = new String(responseBody); //writeXML(str); System.out.println(str); } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有问题 System.out.println("Please check your provided http address!"); e.printStackTrace(); } catch (IOException e) { // 发生网络异常 e.printStackTrace(); } finally { // 释放连接 postMethod.releaseConnection(); } }