少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
 
京-pk发型不乱CEO<wuraolong@126.com>  23:33:00
  public String executeHttpRequest(String url, String requestContent,Log log) {
        Log logHttp= log;  //logHttp对应的可能是开放平台、酒店开放平台、酒店
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        try {
            postMethod.setRequestEntity(new StringRequestEntity(requestContent, "text/xml; charset=" + charSet, charSet));
            postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charSet);
            postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, soTimeOut);
            int statusCode = client.executeMethod(postMethod);
            BufferedReader in = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream(), charSet));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            if (statusCode != HttpStatus.SC_OK) {
                logHttp.error("Method:HttpClientUtil.executeHttpRequest---->请求返回状态不为200,url:" + url + ",返回结果:" + sb.toString());
                return "";
            }
             logHttp.info("Method:HttpClientUtil.executeHttpRequest---->请求返回状态为200,url:" + url + ",返回结果:" + sb.toString());
            return sb.toString();
        }
        catch (Exception e) {
            logHttp.error("Method:HttpClientUtil.executeHttpRequest---->发生异常!请检查网络和参数", e);
        }
        finally {
            postMethod.releaseConnection();
        }
        return null;
    }
}
posted on 2012-08-15 23:43 abin 阅读(1067) 评论(0)  编辑  收藏 所属分类: httpClient

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问