zoukankan      html  css  js  c++  java
  • Apache HttpClient 访问http连接

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.PostMethod;
    
    public class ActionTest {
         public static void main(String[] args) {
                int app = 0;
               PostMethod postMethodProxyRequest = null;
               HttpClient httpClient = null;
                try {
    
                    String url = "http://10.10.129.39:8090/msg/versionDetection" ;
                    postMethodProxyRequest = new PostMethod(url);
                    httpClient = new HttpClient();
                    httpClient.getHttpConnectionManager().getParams().setSoTimeout(
                               1800000);
                    //增加HTTP头信息
                    postMethodProxyRequest.setRequestHeader( "name","L" );
                                        //增加HTTP体信息
                    postMethodProxyRequest.setParameter( "age", "13");
                    //连接请求
                    app = httpClient.executeMethod(postMethodProxyRequest);
                                        //获取返回的HTTP头信息中result的值
                    String result = postMethodProxyRequest.getResponseHeader("result" ).getValue();
                                        //获取HTTP体信息
                    String str=postMethodProxyRequest.getResponseBodyAsString();
                    str = new String(str.getBytes("ISO-8859-1" ),"GBK" );
                    System. out.println( "返回应答结果:" + result);
                    System. out.println( "executeMethod:" + app);
                    System. out.println( "返回应答体:" +str);
    
               } catch (HttpException e) {
                    System. out.println(app);
                    e.printStackTrace();
               } catch (Exception e) {
                    System. out.println(app);
                    e.printStackTrace();
               } finally {
                     if ( null != postMethodProxyRequest) {
                         postMethodProxyRequest.releaseConnection();
                         httpClient.getHttpConnectionManager().closeIdleConnections(0);
                    }
    
               }
         }
    }
  • 相关阅读:
    #树#遍历#N叉树的前序遍历
    #树#递归#最大二叉树II
    #树#递归#二叉树的镜像
    #树#递归#最大二叉树
    #树#二叉搜索树的最近公共祖先
    #树#二叉树的直径
    #树#N叉树的后序遍历
    #树#判断平衡二叉树
    webpack+react+nodejs+express前端开发环境搭建
    sublime 玩转react+es6
  • 原文地址:https://www.cnblogs.com/live365wang/p/2940003.html
Copyright © 2011-2022 走看看