HttpConnectionTool工具类

所需积分/C币:10 2013-08-13 17:57:14 2KB ZIP
23
收藏 收藏
举报

通过HttpConnection请求跨域接口 public String invokeServiceMethod(String url,Map params,Map header) throws Exception{ HttpClient httpclient=new DefaultHttpClient(); HttpResponse response = null; HttpEntity entity = null; httpclient.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); //设置cookie的兼容性 HttpPost httpost = new HttpPost(url); //引号中的参数是:action的地址 if (header!=null&&header.size()>0) { for (String key : header.keySet()) { httpost.setHeader(key,header.get(key)); } } List nvps = new ArrayList (); for (String keyItem : params.keySet()) { String key=keyItem; String value=params.get(keyItem); nvps.add(new BasicNameValuePair(key,value)); } httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); //将参数传入post方法中 response = httpclient.execute(httpost); //执行 StatusLine stateLine=response.getStatusLine(); int statusCode=stateLine.getStatusCode(); if (statusCode!=200) { httpclient.getConnectionManager().shutdown(); return ""; } entity = response.getEntity(); //返回服务器响应 try{ String responseString = null; if (entity != null) { responseString = EntityUtils.toString(entity); } return responseString; } finally { if (httpclient!=null) httpclient.getConnectionManager().shutdown(); // release connection gracefully } }

...展开详情
立即下载
限时抽奖 低至0.43元/次
身份认证后 购VIP低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
  • 分享宗师

关注 私信
上传资源赚钱or赚积分
最新推荐
HttpConnectionTool工具类 10积分/C币 立即下载
1/0