代码里配置java代理

通用 

     //链接代理

        System.setProperty("http.proxyHost", "代理地址"); 
        System.setProperty("http.proxyPort", "端口号");

        //身份验证

        Authenticator authenticator =new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("代理账号", "代理密码".toCharArray());
            }
        };

        Authenticator.setDefault(authenticator);

 

Https请求配置代理

package com.neusiri.qyyx.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.*;
import java.util.Base64;


public class HttpPost {

   public String post(String s_url){
      StringBuffer sb = new StringBuffer();
      HttpURLConnection con = null;

      try {
         URL url = new URL(s_url);
         Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("代理地址",端口号));
         con = (HttpURLConnection) url.openConnection(proxy);
         Authenticator authenticator =new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication("代理账号", "代理密码".toCharArray());
            }
         };
         Authenticator.setDefault(authenticator);
         //con.connect();
         con.setRequestMethod("POST");
         con.setDoOutput(true);
         con.setDoInput(true);
         con.setUseCaches(false);
         con.setRequestProperty("Content-Type",
               "application/x-www-form-urlencoded");
         OutputStreamWriter osw = new OutputStreamWriter(
               con.getOutputStream(), "UTF-8");
         osw.write(sb.toString());
         osw.flush();
         osw.close();
      } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (ProtocolException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } finally {
         if (con != null) {
            con.disconnect();
         }
      }

      StringBuffer buffer = new StringBuffer();
      try {
         BufferedReader br = new BufferedReader(new InputStreamReader(
               con.getInputStream(), "UTF-8"));
         String temp;
         while ((temp = br.readLine()) != null) {
            buffer.append(temp);
            buffer.append("\n");
         }
      } catch (Exception e) {
         e.printStackTrace();
      }

      return buffer.toString();
   }
   //调用
   public static void main(String args[]) throws IOException
   {
      HttpPost hp = new HttpPost();
      System.out.println("开始");
      String res = hp.post("https://...测试地址");
      System.out.println("成功");
   }

}

HTTP请求


    public static HttpClient getHttpClient() throws IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        //读取配置文件里的代理相关信息
        InputStream in = HttpProxy.class.getResourceAsStream("/application.properties");
        Properties properties=new Properties();
        properties.load(in);
        
        String proxyHost = properties.getProperty("proxy.host");
        String proxyPort = properties.getProperty("proxy.port");
        String userName = properties.getProperty("proxy.username");
        String password = properties.getProperty("proxy.password");
        
        System.setProperty("http.proxySet", "true");  
        System.setProperty("http.proxyHost", proxyHost);  
        System.setProperty("http.proxyPort", proxyPort);  
        // 针对https也开启代理  
        System.setProperty("https.proxyHost", proxyHost);  
        System.setProperty("https.proxyPort", proxyPort); 
        
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(proxyHost, Integer.parseInt(proxyPort)),
                new UsernamePasswordCredentials(userName, password));
        HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort));
        httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
        return httpClient;
    }

//调用

 public static void main(String args[]) throws IOException
    {   
        StringBuffer sb = new StringBuffer();
        //创建HttpClient实例
        HttpClient client = getHttpClient();
        //创建httpGet
        List<NameValuePair> params = new ArrayList();
        params.add(new BasicNameValuePair("data", "{\"requirementName\":\"\",\"page\":\"1\",\"rows\":\"10\"}"));
        String strData = "";
        //创建Get请求
        //执行
        try {
            strData = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
            System.out.println(strData);
//            HttpGet httpGet = new HttpGet("http://rp.irichers.com/require/checkReqInforList.do"+"?"+strData);
            HttpGet httpGet = new HttpGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx59a32cbdf0e15ad9&corpsecret=0OjNGfmhYrWAarKcS7oP8dYgMbY25JmiuiINjH5sbsQ");
            HttpResponse response = client.execute(httpGet);
            HttpEntity entry = response.getEntity();

            if(entry != null)
            {
                InputStreamReader is = new InputStreamReader(entry.getContent());
                BufferedReader br = new BufferedReader(is);
                String str = null;
                while((str = br.readLine()) != null)
                {
                    sb.append(str.trim());
                }
                br.close();
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(sb.toString());
    }
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值