通用
//链接代理
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());
}