HttpClient PostMethod方式

本文介绍了两种使用HttpClient进行POST请求的方法:一种是普通的POST请求,包括设置请求头、添加参数等步骤;另一种是携带文件上传的POST请求,涉及使用MultipartRequestEntity封装文件和普通参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、PostMethod一般请求

    /**
     * POST方式
     * @return
     * @throws Exception
     */
    public static String PostMethodTest() throws Exception{
        System.out.println("开始");

            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(URI);
            try{

                method.addRequestHeader(new Header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") );    

    //          method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

                method.addParameter(new NameValuePair("appid", "XXXX") );
                method.addParameter(new NameValuePair("appkey", "XXXX") );

    //          method.addParameter("appid", "XXXX");
    //          method.addParameter("appkey", "XXXX");

                int result = client.executeMethod(method);
                if (result == HttpStatus.SC_OK) {
                    InputStream in = method.getResponseBodyAsStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    return URLDecoder.decode(baos.toString(), "UTF-8");
                } else {
                    throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
                }
            }finally {
                method.releaseConnection();
            }

    }

二、PostMethod带文件方式

    /**
     * POST方式 传带文件的调用
     * @return
     * @throws Exception
     */
    public static String PostMethodFileTest() throws Exception{
        System.out.println("开始");

            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(URI);
            try{

                FilePart filePart = new FilePart("file",new File("D:\\8\\5972-41-2017-06-07-1440-16406.wav"));//文件参数
                StringPart questionId = new StringPart("questionId","10001");//普通参数
                StringPart userId = new StringPart("userId","765709");//普通参数
                StringPart homeworkId = new StringPart("homeworkId","950");//普通参数

                Part[] parts ={filePart,questionId,userId,homeworkId};
                MultipartRequestEntity mre=new MultipartRequestEntity(parts ,method.getParams());    //封装了普通字段和文件字段
                method.setRequestEntity(mre );
                int result = client.executeMethod(method);
                if (result == HttpStatus.SC_OK) {
                    InputStream in = method.getResponseBodyAsStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    return URLDecoder.decode(baos.toString(), "UTF-8");
                } else {
                    throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
                }
            }finally {
                method.releaseConnection();
            }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值