zoukankan      html  css  js  c++  java
  • HttpClient GET和POST请求

    package com.rogue.hclient;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.RequestEntity;
    import org.apache.commons.httpclient.methods.StringRequestEntity;
    
    /**
     * 测试HttpClient功能
     * @author djoker
     *
     */
    public class HClientTest {
    
        HttpClient client = new HttpClient();
        
        //get功能测试
        public void getTest(){
            String uri = "http://172.16.100.20/cgi-bin/ht.cgi?method=getMethodTest";
            GetMethod method = new GetMethod(uri);
            try {
                int code = client.executeMethod(method);
                System.out.println(code);
                if(200 == code){
                    
    //                StringBuffer sb = new StringBuffer();
    //                sb.append(method.getResponseBodyAsString());    //不推荐使用,会有警告,如果读取的内容过多,会导致超过最大读取值
    //                System.out.println(sb.toString());
                    
                    InputStream is = method.getResponseBodyAsStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
                    String line = null;
                    while((line = br.readLine()) != null){
                        System.out.println(line);
                    }
                }
            } catch (HttpException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        //POST测试
        public void postTest(){
            String uri = "http://172.16.100.20/cgi-bin/ht.cgi";
            String content = "method=PostMethod&paramer=paramer";   //参数
            PostMethod method = new PostMethod(uri);
            RequestEntity requestEntity = new StringRequestEntity(content); //字符串请求参数
            method.setRequestEntity(requestEntity); //设置请求参数
            try {
                int code = client.executeMethod(method);
                System.out.println(code);
                if(200 == code){
                    InputStream is = method.getResponseBodyAsStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
                    String line = null;
                    while((line = br.readLine()) != null){
                        System.out.println(line);
                    }
                }
            } catch (HttpException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args){
            HClientTest hct = new HClientTest();
            hct.getTest();
            System.out.println("--------");
            hct.postTest();
        }
    }
  • 相关阅读:
    【Machine Learning in Action --4】朴素贝叶斯过滤网站的恶意留言
    【Machine Learning in Action --4】朴素贝叶斯分类
    【python问题系列--3】TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
    python多行注释
    CentOS中文件夹基本操作命令
    Centos下载文件wget命令详解
    python--zeros函数和ones函数
    【python问题系列--2】脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
    【Machine Learning in Action --3】决策树ID3算法预测隐形眼睛类型
    php 利用http上传协议(表单提交上传图片 )
  • 原文地址:https://www.cnblogs.com/djoker/p/6915514.html
Copyright © 2011-2022 走看看