HttpClient

看了几天的HttpClient,写了一点代码,留到以后用。

使用的是common-httpclient.jar

  1 public void SendHttpRequest(){
  2         PostMethod method=null;
  3         String url="http://localhost:8080/com.isoftstone.iics.bizsupport.epps/epolicy/create";
  4         String xmlString="";
  5         method=new PostMethod(url);
  6         client.getParams().setContentCharset("utf-8");
  7         try
  8         {
  9             client.getHttpConnectionManager().getParams().setConnectionTimeout(6000);
 10             RequestEntity entity=new StringRequestEntity(xmlString,"text/xml","utf-8");
 11             method.setRequestEntity(entity);
 12             client.executeMethod(method);
 13             String response=method.getResponseBodyAsString();
 14             System.out.println(response+"------");
 15             method.releaseConnection();
 16         } catch (Exception e)
 17         {
 18             // TODO Auto-generated catch block
 19             e.printStackTrace();
 20         }
 21 //        RequestEntity entity = new StringRequestEntity(xmlString, "text/xml", encoding);
 22     }
 23     public void SendBaidu(){
 24         //创建HttpClient实例
 25         /*HttpClientParams params=new HttpClientParams();
 26         params.setConnectionManagerTimeout(6000);
 27         params.setContentCharset("utf-8");
 28         HttpClient client=new HttpClient(params);*/
 29         HttpClient client=new HttpClient();
 30 //        HttpClient client=new HttpClient(new MultiThreadedHttpConnectionManager());
 31 //        HttpClient client=new HttpClient(new SimpleHttpConnectionManager());
 32         String url="https://www.baidu.com/index.php?tn=monline_3_dg";
 33 //        HttpMethod method=new PostMethod(url);
 34 //        HttpMethod method=new GetMethod(url);
 35         PostMethod method=new PostMethod(url);
 36         String charset="utf-8";
 37         method.getParams().setContentCharset(charset);
 38 //        method.addRequestHeader("Context-Type", "application/x-www-form-urlencoded;utf-8");
 39         client.getHttpConnectionManager().getParams().setConnectionTimeout(6000);
 40     /*    RequestEntity requestEntity=null;
 41         try
 42         {
 43             requestEntity = new StringRequestEntity(null,null,null);
 44         } catch (UnsupportedEncodingException e1)
 45         {
 46             e1.printStackTrace();
 47         }
 48         method.setRequestEntity(requestEntity);*/
 49     /*    File newFile=new File("d:/axtmp/111.txt");
 50         if(!newFile.exists()) {
 51             
 52         }
 53         try
 54         {
 55              // FilePart:用来上传文件的类,file即要上传的文件
 56             FilePart fp=new FilePart("file",newFile);
 57             Part[] parts={fp};
 58              // 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
 59             MultipartRequestEntity mre=new MultipartRequestEntity(parts,method.getParams());
 60             method.setRequestEntity(mre);
 61         } catch (FileNotFoundException e1)m
 62         {
 63             e1.printStackTrace();
 64         }*/
 65         /*NameValuePair user=new NameValuePair("loginUser","fenghao");
 66         NameValuePair ps=new NameValuePair("loginps","ps");
 67         NameValuePair[] data={user,ps};
 68         method.setRequestBody(data);*/
 69         int statu=0;
 70         try
 71         {
 72             statu=client.executeMethod(method);
 73 //            HttpStatus类是为返回值定义常量
 74             /*if(statu==HttpStatus.SC_OK){
 75                 InputStream in=method.getResponseBodyAsStream();
 76                 BufferedReader br=new BufferedReader(new InputStreamReader(in));
 77                 StringBuffer str=new StringBuffer();
 78                 String s="";
 79                 while((s=br.readLine())!=null){
 80                     str.append(s);
 81                 }
 82                 System.out.println(str.toString());
 83             }*/
 84         } catch (HttpException e)
 85         {
 86             e.printStackTrace();
 87         } catch (IOException e)
 88         {
 89             e.printStackTrace();
 90         }
 91         System.out.println(statu+"--responseExecute");
 92         String response=null;
 93         try
 94         {
 95             response=method.getResponseBodyAsString();
 96             System.out.println(method.getStatusCode()+"code");
 97             System.out.println(method.getStatusLine()+"line");
 98             System.out.println(method.getStatusText()+"text");
 99         } catch (IOException e)
100         {
101             e.printStackTrace();
102         }
103         System.out.println(response+"--responseBody");
104         method.releaseConnection();
105 
106     
107     }

HttpClient就先看到这把!

http://www.cnblogs.com/ITtangtang/p/3968093.html(在这看的代码,写的特别好,很受用)

posted @ 2016-04-20 16:36  默默行走  阅读(359)  评论(0编辑  收藏  举报