package com.cic.interfacemodule.util; import cn.hutool.http.HttpRequest; import sun.net.www.protocol.http.HttpURLConnection; import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.cic.common.util.MyUtils; /** * @author: xuedr * @date: 2017-11-27 15:48 * @description: */ public class HttpUtils { private static boolean fiddlerDebug = false; private static String fiddlerIp = "localhost"; private static int fiddlerPort = 8888;
/** *加代理方法 * */
public static String postWithParamsForString(String url, List<NameValuePair> params) { HttpHost proxy = new HttpHost("10.192.166.179", 8080, "http"); HttpPost httpPost = new HttpPost(url); RequestConfig defaultRequestConfig = RequestConfig.custom() .setProxy(proxy).build(); httpPost.setConfig(defaultRequestConfig); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultRequestConfig(defaultRequestConfig).build(); String s = ""; try { httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); HttpResponse response = httpclient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); s = EntityUtils.toString(entity); } } catch (IOException e) { e.printStackTrace(); } return s; }
<
Http请求接口工具类
最新推荐文章于 2024-06-24 13:18:31 发布