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 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 _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 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 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();
    }
}

 

你可能感兴趣的:(java后端Post请求)