HttpClient调用接口,发送键值对参数

package common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import ewebeditor.admin.main_jsp;
import net.sf.json.JSONArray;

public class Test {
    public void requestByPostMethod() {
        CloseableHttpClient httpClient = getHttpClient();
        try {
            HttpPost post = new HttpPost("http://59.195.128.222:88/accept/service/SubmitResult");
            // 创建参数列表
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            list.add(new BasicNameValuePair("ID", "1408528201809100028"));
            list.add(new BasicNameValuePair("Result", "1"));
            list.add(new BasicNameValuePair("Opinion", "通過"));
            list.add(new BasicNameValuePair("LicenceCode", ""));
            list.add(new BasicNameValuePair("LicenceName", ""));
            list.add(new BasicNameValuePair("SystemCode", "B59B876284C441A5BAFD944A86F7648D"));            
            String name = "方子君";
            String time = "2018-09-17 17:09:00";
            Base64 base64 = new Base64();
            try {
                list.add(new BasicNameValuePair("DeliverUser",
                        base64.encodeToString(name.getBytes("UTF-8")).replaceAll("[\\s*\t\n\r]", "")));
                list.add(new BasicNameValuePair("DeliverTime",
                        base64.encodeToString(time.getBytes("UTF-8")).replaceAll("[\\s*\t\n\r]", "")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            List list1 = new ArrayList();
            JSONArray json = JSONArray.fromObject(list1);
            list.add(new BasicNameValuePair("fileJson", json.toString()));
            // url格式编码
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list, "UTF-8");
            post.setEntity(uefEntity);
            HttpEntity entity2 = post.getEntity();
            System.out.println("=====================" + EntityUtils.toString(entity2, "utf-8")); // 获取网页内容
            System.out.println("POST 请求...." + post.getURI());
            // 执行请求
            CloseableHttpResponse httpResponse = httpClient.execute(post);
            try {
                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                System.out.println("result:" + result);
            } finally {
                httpResponse.close();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                closeHttpClient(httpClient);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private CloseableHttpClient getHttpClient() {
        return HttpClients.createDefault();
    }

    private void closeHttpClient(CloseableHttpClient client) throws IOException {
        if (client != null) {
            client.close();
        }
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.requestByPostMethod();
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTTP状态码302表示重定向,服务器向客户端返回该状态码时,意味着客户端需要重新发送请求到新的URL地址。所以,在您的情况下,服务器可能返回了302状态码,要求您的客户端重新发送请求到新的URL地址。 您可以尝试使用HttpClient的RedirectStrategy来处理重定向。以下是一个示例代码,展示如何在HttpClient中使用RedirectStrategy: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.util.EntityUtils; import java.io.IOException; public class HttpClientExample { public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.custom() .setRedirectStrategy(new LaxRedirectStrategy()) .build(); HttpGet httpGet = new HttpGet("http://www.example.com"); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } } } ``` 在上面的示例代码中,我们使用了 `LaxRedirectStrategy` 来处理重定向。`LaxRedirectStrategy` 会自动重定向所有的HTTP方法,包括POST、PUT等。如果您只需要重定向GET请求,可以使用 `DefaultRedirectStrategy`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值