我在请求一个别的网站接口数据的时候,设置了代理,但是报错405,具体信息如下,我查了很多信息说是把POST改成GET就可以了,这个对我的代码没有任何作用,最令人头疼的是如果我仅仅是把“HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(proxy);”中的“proxy”去掉的话,代码就会成功运行,没有任何报错,并且可以成功拿到想要的数据,我想知道要怎么样才能够用代理访问成功
URL realUrl = new URL(Constant.Url.POWER);
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("3.23.44.179",80));
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(proxy);
//设置消息头
setConnParam(conn);
//获取参数
String param = Tool.getResourceString(Constant.Path.PARAM);
//获取返回体
String result = Tool.getReturnBody(conn,param);
if(result == null || "".equals(result.trim())){
System.out.println("未获取到信息!");
return;
}
response.getWriter().write(result);
//工具类报错代码
public static String getReturnBody(HttpURLConnection conn, String param) throws IOException {
String result = "";
PrintWriter out = null;
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
if(param == null){
param = "";
}
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
}
---------------------------------
//控制台报错
java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 405 Method Not Allowed"
at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2085) ~[na:1.8.0_101]
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183) ~[na:1.8.0_101]
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1283) ~[na:1.8.0_101]
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1258) ~[na:1.8.0_101]
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250) ~[na:1.8.0_101]
at com.gua.king.utils.Tool.getReturnBody(Tool.java:120) ~[classes/:na]
at com.gua.king.controller.mainController.getPower(mainController.java:176) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.15.jar:5.3.15]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.15.jar:5.3.15]
很明显就是你的代理这块设置的有问题.你的代理是购买的还是网上找的,感觉这个代理的设置不太对劲,你debug看下在new Proxy的时候是不是就已经报错了