xlflf

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

项目使用的编码是utf-8,webserver项目使用的是gb2312

get方式

String sHost = "1.76.0.1"; //访问服务器的主网址
  HttpHost httpHost = new HttpHost(sHost,8080);

String sURL="/Service1.asmx/GetPaxFlightInfo?paxMessage="; //定义你需要访问的网址后面的路径     
//        HttpGet httpGet = new HttpGet(sURL);//定义get请求
//        HttpClient httpclient = new DefaultHttpClient();
//        HttpResponse httpResponse = httpclient.execute(httpHost, httpGet); //使用httpclient执行get请求并返回响应  访问的网址为 www.hao123.com/abc/index.html
//        // 2 获取响应
//  HttpEntity responseEntity = httpResponse.getEntity();
//  String sReturnHtml = getResponseContent(responseEntity);//得到get请求返回的html页面,然后就做你想做的东西了
//  System.out.println(sReturnHtml);

post方式

String sHost = "1.76.0.1"; //访问服务器的主网址
  HttpHost httpHost = new HttpHost(sHost,8080);
  String url="/Service1.asmx/GetPaxFlightInfo";
  params.add(new BasicNameValuePair("paxMessage", "{\"IDNO\":\"440682197812286026\",\"paxName\":\"\",\"orginCode\":\"KHN\",\"IDType\":\"NI\"}"));
  HttpClient hc = new DefaultHttpClient();
  String result = "调用请求超时!";
   // Post请求
   HttpPost httpPost = new HttpPost(url);
   String sCharSet="GB2312";
   // 设置参数
   httpPost.setEntity(new UrlEncodedFormEntity(params,sCharSet));
   // 发送请求
   HttpResponse httpResponse = hc.execute(httpHost, httpPost);
      HttpEntity responseEntity = httpResponse.getEntity();
      result = getResponseContent(responseEntity);

 

public String getResponseContent(HttpEntity responseEntity)
   throws Exception {
  byte[] bytes = EntityUtils.toByteArray(responseEntity);
                  String sCharSet="GB2312";       //这里可以定义你指定的编码,中文网站编码一般为GB2312
  return new String(bytes, sCharSet);
 }

posted on 2013-06-26 18:03  xlflf  阅读(592)  评论(1)    收藏  举报