Reputation:
I am getting this error when I am try to make url connection through proxy
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(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.test.sslpost.main(sslpost.java:81)
It produce error while opening connection and if I am trying with out proxy its working fine.
Please see the java code described bellow
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class sslpost {
public static void main(String[] args) {
try {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
} };
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
System.setProperty("https.proxySet", "true");
System.setProperty("https.proxyHost", "xxx.xxx.xxx.xxx");
System.setProperty("https.proxyPort", "80");
URL url = new URL("https://www.google.com");
@SuppressWarnings("deprecation")
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setFollowRedirects(true);
String query = "serviceId="
+ URLEncoder.encode("7");
connection.setRequestProperty("Content-length",
String.valueOf(query.length()));
connection.setRequestProperty("Content-Type",
"application/x-www- form-urlencoded");
connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)");
DataOutputStream output = new DataOutputStream(
connection.getOutputStream());
output.writeBytes(query);
System.out.println("Resp Code:" + connection.getResponseCode());
System.out.println("Resp Message:"
+ connection.getResponseMessage());
DataInputStream input = new DataInputStream(
connection.getInputStream());
for (int c = input.read(); c != -1; c = input.read())
System.out.print((char) c);
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 2
Views: 8222
Reputation: 1792
Open C:\Users\yourUsername\.gradle
, you might find a Gradle.build
file that contains your proxy settings.
Delete it and try again, if you don't find the file it might be an internet connection issue
Upvotes: 1
Reputation: 41200
I think there is no need of code change. This means proxy server is not able to tunnel
you must try with different https proxy...
systemProp.https.proxyHost=""
systemProp.https.proxyPort=""
systemProp.https.proxyPassword=""
systemProp.https.proxyUser=""
Upvotes: 0
Reputation: 21
Did you called "super.doGet(req, resp);" in your code? Such as below?
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
I is able to check the implementation of super.doGet, then you can found the below code.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
Upvotes: 1
Reputation: 6193
HTTPS uses port 443 by default, where HTTP uses port 80 by default.
In your code you have this
System.setProperty("https.proxyPort", "80");
perhaps try set that to 443 instead of 80
Upvotes: 1