ladyABC 发表于 2012-09-11 23:46

大家代码里都怎么发起http请求的~求深入

写了一个最简单的demo,用来发起http请求调用。
大家代码里都怎么个搞法?
求深入,求扩展~~        public static void main(String[] args) throws HttpException, IOException {
                testHttp(0);
                testHttp(1);
        }
       
        /**
       * type=0, get
       * type=1, post
       * http://haoma.imobile.com.cn/index.php?mob=18858111994
       */
        public static void testHttp(int type) throws HttpException, IOException{
                HttpClient client = new HttpClient();
                client.getHostConfiguration().setHost("haoma.imobile.com.cn", 80, "http");
                HttpMethod method = null;
                switch(type){
                case 0:
                        method = getGetMethod();
                        break;
                case 1:
                        method = getPostMethod();
                        break;
                }
               
                client.executeMethod(method);
                System.out.println("statusLine = " + method.getStatusLine());

                String response = new String(method.getResponseBodyAsString());
                System.out.println("response = " + response);
               
                method.releaseConnection();
        }

        private static HttpMethod getGetMethod() {
                return new GetMethod("/index.php?mob=18858111994");
        }

        private static HttpMethod getPostMethod() {
                PostMethod post = new PostMethod("/index.php");
                NameValuePair simcard = new NameValuePair("mob", "18858111994");
                post.setRequestBody(new NameValuePair[] { simcard });
                return post;
        }

lloydm 发表于 2012-09-16 15:23

device_memory 发表于 2012-09-25 10:48

swt               
页: [1]
查看完整版本: 大家代码里都怎么发起http请求的~求深入