java使用http代理访问服务器

    技术2023-05-10  75

    上工具类,亲测有效

    1.创建代理地址(公共方法、所有代理引用)

    public static HttpHost addProxy(){ //代理地址、端口号、协议 return new HttpHost("9.236.225.252", 8080, "http"); //添加代理 }

    2. get请求 (带参数)

    public static String doGet(String url, Map<String, String> param) { // 创建代理地址 HttpHost proxy = addProxy(); //账号密码 =="ap1","adpd65" BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(new AuthScope(proxy),new UsernamePasswordCredentials("ap1","adpd65")); //设置账号密码 CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build(); String resultString = ""; CloseableHttpResponse response = null; try { // 创建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); //设置代理配置 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); httpGet.setConfig(config); // 执行请求 response = httpclient.execute(httpGet); logger.info("代理返回结果======"+response); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } logger.info("华为云ocr返回结果" + resultString); } catch (Exception e) { logger.error("总公司华为云ocr接口异常" + e); } finally { try { if (response != null) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; }

    3. post请求:带token,带参数

    public static String doPost(String url, Map<String, String> param, String token) { logger.info("开始进行识别"); logger.info("--url:{},---para:{},---token:{}"+url+param+token); //添加代理 HttpHost proxy = addProxy(); //账号密码 BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(new AuthScope(proxy),new UsernamePasswordCredentials("app1","adpp#1d65")); //设置账号密码 CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build(); CloseableHttpResponse response = null; String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); if (!StringUtils.isEmpty(token)) { httpPost.setHeader("Authorization", token); } // 创建参数列表 if (param != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, (String) param.get(key))); } // 模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8"); httpPost.setEntity(entity); } RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); //设置代理配置 httpPost.setConfig(config); // 执行http请求 response = httpClient.execute(httpPost); logger.info("代理返回结果======"+response); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); logger.info("返回结果" + resultString); } catch (Exception e) { logger.error("接口异常" + e); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; }

    4. post请求,带json数据

    //发送json数据 public static String doPostJson(String url, String json) { //创建代理 HttpHost proxy = addProxy(); // 创建带有账号密码的Httpclient对象 BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(new AuthScope(proxy),new UsernamePasswordCredentials("app1","adpp#1d65")); CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build(); CloseableHttpResponse response = null; String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建请求内容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); //设置代理配置 RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); httpPost.setConfig(config); // 执行http请求 response = httpClient.execute(httpPost); logger.info("代理返回结果======"+response); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; }

    以上三种代理方法,可以自由变更为自己需要的业务场景

    Processed: 0.017, SQL: 9