I am trying to get Proxy working with Socket. But everytime I tried, it would returned a "Exception in thread "pool-1-thread-1" java.lang.IllegalArgumentException: Invalid Proxy" exception error
at java.net.Socket.(Socket.java:131)
But if its Proxy.Type.SOCKS, it works.
public void Test()
{
Socket s = null;
SocketAddress addr = null;
Proxy proxy = null;
addr = new InetSocketAddress("127.0.0.1", 8080);
proxy = new Proxy(Proxy.Type.HTTP, addr);
socket = new Socket(proxy); // This is the line that is triggering the exception
}
Sadly this is a bug in (Oracle) Java - only DIRECT and SOCKS proxy is supported for Socket. See http://bugs.sun.com/view_bug.do?bug_id=6370908.
Related
I'm using java.net.URL and java.net.Proxy to load my app's data from the desired URL. When I am using Proxy.Type.SOCKS proxy, everything is OK, but I need to use Proxy.Type.HTTP. My code is the following:
URL url = new URL("https://google.com/");
final Proxy proxy = getProxy();
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Where getProxy (simplified) looks as follows:
private static Proxy getProxy(){
if(!StringUtils.isNullOrBlank(username)){
Authenticator.setDefault(new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
} else {
Authenticator.setDefault(null);
}
return new Proxy(Proxy.Type.SOCKS HTTP, new InetSocketAddress(address, port));
}
I am getting java.io.IOException: unexpected end of stream on null, Caused by: java.io.EOFException: \n not found: size=0 content=....
System.setProperty option doesn't seem to work, as well.
Is there any way to use HTTP proxy correctly on Android?
I'm using Burp's proxy server which is running in background.
I have inserted the cacert.der into my java trust store to which my eclipse is pointing to.
Now I have the following code :
...........................................................................
SslContextFactory sslContextFactory = new SslContextFactory();
HttpClient httpClient = new HttpClient(sslContextFactory);
httpClient.start();
Address address = new Address("127.0.0.1",8082);
Socks4Proxy socks4Proxy = new Socks4Proxy(address, true);
proxyConfiguration.getProxies().add(socks4Proxy);
Request request = httpClient.newRequest(
new URI("https://google.com"));
request.method(HttpMethod.GET);
ContentResponse contentResponse = request.send();
System.out.println(contentResponse.getContentAsString());
...........................................................................
The above code is giving me the following error :
Exception in thread "main" java.util.concurrent.ExecutionException: java.io.IOException: SOCKS4 tunnel failed with code 84
at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118)
at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101)
at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:653)
at Get.main(Get.java:43)
...............................................................................
Can anyone suggest me how to resolve this ??
I am trying to connect to an external server from Java (JDK5) server, though proxy.
https://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html (3rd section).
java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, new java.net.InetSocketAddress("WHQPROXYPOOL", 80));
java.net.HttpURLConnection con = (java.net.HttpURLConnection) new java.net.URL("https://performancemanager8.successfactors.com/odata/v2/FOPayGroup?$format=json").openConnection(proxy);
But I am getting error, "UnsupportedOperationException, Method not implemented".
When I check Java class (JDK5) URLStreamHandler
protected URLConnection openConnection(URL u, Proxy p) throws IOException {
throw new UnsupportedOperationException("Method not implemented.");
}
How can I connect to target server through proxy?
Try this #RaghuVamseedhar:
java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, new java.net.InetSocketAddress("WHQPROXYPOOL", 80));
java.net.HttpURLConnection con = (java.net.HttpURLConnection) new java.net.URL(null, "https://performancemanager8.successfactors.com/odata/v2/FOPayGroup?$format=json", new sun.net.www.protocol.http.Handler()).openConnection(proxy);
I got this code to get the default sun HTTP Handler here
I have a Java servlet which tries to connect to the source(Using request ip address).
Method is as :
String ip = request.getRemoteAddr();
private void connect(String ip) throws SocketException, IOException {
Socket socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(new InetSocketAddress(ip, Constant.PORT));
}
Now if it doesn't connect withint a second it should throw exception but it is not throwing exception in one second but takes a while like 10-15 seconds.
Could someone help why this is happening?
SO_TIMEOUT (which is set by socket.setSoTimeout) only affects socket.getInputStream().read(). To specify connect timeout, specify a second parameter to socket.connect:
socket.connect(new InetSocketAddress(ip, Constant.PORT), 1000);
Good evening SO
I'm trying to make wonderful piece of code that can retrieve message over the POP protocol. I've so far followed the RFC1939 (POP Specification).
And actually it's working with my web host's POP3 server (which is not over SSL).But when accessing GMail all I get is empty responses :(
I assume it's the SSL part that's "breaking" it.
In my "open" function that creates the Socket I have this:
public void open() throws UnknownHostException, IOException
{
if(this.SSL)
{
SSLSocketFactory fac = (SSLSocketFactory) SSLSocketFactory.getDefault();
this.s = fac.createSocket(this.in_host, this.in_port);
}
else
{
this.s = new Socket(this.in_host, this.in_port);
}
this.out = new PrintWriter(s.getOutputStream(), true);
this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
where SSL is a bool indicating the connection should be over SSL, and s is the Socket. in_host and in_port is the host (pop.gmail.com) and port (995). out and in is the streams I write and read to/from.
One of the worst parts is that it doensn't throw any errors. Where I normally get the response, I just get an empty string.
Question is: How do I connect to a POP server over SSL/TLS, preferably the Gmail server?
What about something like this:
public void connect() throws Exception {
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.port", "995");
pop3Props.setProperty("mail.pop3.socketFactory.port", "995");
URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
username, password);
session = Session.getInstance(pop3Props, null);
store = new POP3SSLStore(session, url);
store.connect();
}
See this article for more info.
declare socket s as SSLSocket . Then it will work