作者:踏实小青年 | 来源:互联网 | 2023-05-18 21:57
*代理设置*HttpClienthttpclientnewDefaultHttpClient();HttpHostproxynewHttpHost(System.getProp
/*代理设置*/
HttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort")));
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
/*代理设置*/-
/*注意引入的类必须是-org.apache.http.HttpHost*/
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort"))));
HttpURLConnection httpURLCOnnection= (HttpURLConnection) url.openConnection(proxy);
我的测试方法是放到外网服务器测试的,通过代理向微信发送信息,下面的方法涉及一些基础信息没有呈现,关键是几个关键语句,有这些关键语句即可。
此外,可以将一些信息添加到tomcat数据库的catalina.sh文件中
就可以不在程序中写死代理IP了
JAVA_OPTS="-server -Xms512m -Xmx512m -XX:PermSize=64M -XX:MaxPermSize=128m "
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxySet=true "
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyHost=代理IP
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyPort=代理端口
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort"))));
我的程序是写死的,这样便于大家借鉴
public String test() throws IOException{
String url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+access_token;
URL getUrl = new URL(url);
// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("代理IP", 8080)); //关键语句
HttpURLConnection cOnnection= (HttpURLConnection) getUrl.openConnection(proxy); //关键语句
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
// 服务器
connection.setConnectTimeout(25000);//关键语句
connection.setReadTimeout(25000);//关键语句
connection.connect(); //关键语句
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String lines;
String jsOnString= "";
while ((lines = reader.readLine()) != null) {
jsOnString= jsonString + lines;
}
reader.close();
// 断开连接
connection.disconnect();
System.out.println("微信返回的json串" + jsonString);
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse respOnse= (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
jsOnString=jsonString.replace("<", "<");
jsOnString=jsonString.replace(">", ">");
request.setAttribute("str",jsonString);
request.setAttribute("str2", "系统没问题");
return "success";
}
/*代理设置*/
HttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort")));
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
//原来的语句
//HttpURLConnection httpURLCOnnection= (HttpURLConnection) url.openConnection();
/*代理设置*/-
/*注意引入的类必须是-org.apache.http.HttpHost*/
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort"))));
HttpURLConnection httpURLCOnnection= (HttpURLConnection) url.openConnection(proxy);