java后端Post请求

后端Post请求

package com.lingxiang.expression.service;


import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class testService {
    public String re(String lastName){
        String url="https://xxxxx";
        String charset="utf-8";
        boolean pretty=false;
        Map<String,Object> map=new HashMap<>();
        map.put("url","http://static.extremevision.com.cn/donkey_0df8ddf6-5c0c-4ce7-9543-9922a21d7f32.jpg");
        String resault=doPost(url,map,charset,pretty);
        return resault;
    }
    public static String doPost(String url, Map<String, Object> _params, String charset, boolean pretty) {
        if(charset==""){
            charset="utf-8";
        }
        StringBuffer response = new StringBuffer();
        org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
        PostMethod method = new PostMethod(url);
        //设置请求头
        //method.setRequestHeader("Authorization","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5jdm1hcnQubmV0L2FwaS9hdXRob3JpemF0aW9ucyIsImlhdCI6MTU1NDczMjE3MSwiZXhwIjoxNTU0NzM5MzcxLCJuYmYiOjE1NTQ3MzIxNzEsImp0aSI6Ik1IaVRlQnZUUVdYOVI1NkYiLCJzdWIiOjI1MjksInBydiI6ImY2YjcxNTQ5ZGI4YzJjNDJiNzU4MjdhYTQ0ZjAyYjdlZTUyOWQyNGQifQ.tE4yD28wHuLNChfmCzffIPzG_oraiJKvzAqR3m4xFjw");
        if (_params != null) {
            for (Map.Entry<String, Object> entry : _params.entrySet()) {
                //设置请求参数
                method.setParameter(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }

        // 设置Http Post数据  方法二
//        if(_params != null) {
//            NameValuePair[] pairs = new NameValuePair[_params.size()];//纯参数了,键值对
//            int i = 0;
//            for (Map.Entry<String, Object> entry : _params.entrySet()) {
//                pairs[i] = new NameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
//                i++;
//            }
//            method.addParameters(pairs);
//        }
        try {
            client.executeMethod(method);
            if (method.getStatusCode() == HttpStatus.SC_OK) {
                // 读取为 InputStream,在网页内容数据量大时候推荐使用
                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),
                        charset));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (pretty)
                        response.append(line).append(System.getProperty("line.separator"));
                    else
                        response.append(line);
                }
                reader.close();
            }
        } catch (IOException e) {
            System.out.println("执行HTTP Post请求" + url + "时,发生异常!");
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        }
        System.out.println("--------------------" + response.toString());
        return response.toString();
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值