HttpUrlConnection timeout even though page is available in the browser - JBoss issue? - java

I'm trying to get page content from a specific URL:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
LOGGER.info("Response code: "+conn.getResponseCode();
... (rest of the code, not relevant at the moment) ...
I'm not able to get conn.getResponseCode(), I have timeout before it gets printed. Surprisingly, when I put the very same URL in my browser, the page will be loaded correctly. I thought it may have something do to with HTTP/HTTPS, unfortunately, after changing HttpURLConnection to HttpsURLConnection there is no difference.
Maybe it has something to do with JBoss configuration? I use JBoss 7.1.1 Final.

Is this an HTTPS url? If it is, you need to account for that. When using an HttpsURLConnection, you must account for the fact that HTTPS certificates need a truststore to compare against. To ignore this sense of trust (and somewhat ruin the point of using HTTPS), see Whats an easy way to totally ignore ssl with java url connections?

Related

orbot as proxy with jaunt web-scraping

I am working on app (android) that is supposed to visit website & retrieve some data from there. I was thinking it would be nice to use tor so I would not leave so much info about myself.
I've completed scraping part and everything works. Problem is, i can not make proxy accessing internet. I wanted to use Orbot as proxy. I always get 405 error and text:
"this is an http connect tunnel, not a full http proxy it appears you have configured your browser to use this tor port as an http proxy this is not correct: this port is configured as connect tunnel, not an http proxy. please configure your client accordingly. you can also use https; then the client should automatically use http connect"
Code:
UserAgent agent = new UserAgent();
agent.setProxyHost("127.0.0.1");
agent.setProxyPort(8118);
agent.visit("http://stackoverflow.com");
I've tried
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8118");
instead of the two middle lines from previous code as well plus few other probably not smart things (: and nothing has helped.
From error text about connect tunnel and proxy I figured problem is somewhere in networking but although I searched about it, I couldn't fix it. Perhaps something in Orbot's settings?
My question is, what am I missing please? :)

Get Host and port inside an EJB Stateless bean installed in my Websphere server

I have an EJB Stateless Bean that is executed automatically by an Scheduler inside Websphere. My EJB is installed in Websphere. Inside my EJB I would like to make an http request to a webservice exposed by other application (inside de same Server). I tried just using relative path because that is how I usually make requests between applications, but inside my bean I don't know how to get the host name and port to build the URL.
I managed to get the host (ip) by doing this:
InetAddress.getLocalHost().toString();
But I also require the port number. The question is: how to get the host and port number of the application server (Websphere) where my EJB application is installed? Is this possible?
This code is how I try to make the request, but this does not work because I need the full path:
URL url = new URL("/MyOtherAppName/myservice");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoOutput(true);
-edit 1 ----
I think the approach of user Gas is nice, and makes my application easier to configure.
I followed this guide to implement the URL Provider approach:
URL Provider Websphere
I'd suggest different approach, rather than getting server host and port use the URL resource and then get it in the EJB. It will be more portable across servers and also more flexible in case you ever move your service somewhere else.
See Using URL resources within an application
Define URL via console in app server, and use #Resource annotation in your EJB like this:
#Resource(name="serviceURL", lookup="url/myurl")
URL myURL;

What is returned from a java.net.URLConnection with a HTTPS URL? Is the result encrypted?

I am using java.net.URLConnection, many of the URLs that I am passing into it use the HTTPS protocol. Is it safer to make the request using the HttpsURLConnection class or if it's still possible to get the benefit of encryption if one uses the URLConnection class.
Not a real question. You don't get the choice. The URLConnection returned by URL.openConnection() for HTTPS: URLs is an HttpsURLConnection.
If you connect a URLConnection to an https URL, it will either connect using HTTPS or produce an error as documented.

Java Webstart and URLConnection caching API

The description of the URLConnection caching API states as the last sentence:
There is no default implementation of URLConnection caching in the Java 2 Standard Edition. However, Java Plugin and Java WebStart do provide one out of the box.
Where can I find more information about the Webstart ResponseCache?
Which Versions of Webstart on which platforms activate Caching?
In which cases is it active? Only HTTP Get?
Can it be configured?
Is the sourcecode available?
Background:
Case 1
With following (groovy) code
def url = new URL('http://repo1.maven.org/maven2/')
def connection = url.openConnection()
def result = connection.inputStream.text
I would expect that every time the code is executed the server is contacted. But when executed in
Java Web Start 10.9.2.05
JRE-Version verwenden 1.7.0_09-b05 Java HotSpot(TM) Client VM
the behavior is different. The first time the code is executed, the server is contacted. All subsequent executions of the code don't involve any communication to the server (traced using wireshark).
But it gets even stranger. After re-start of the webstart app, the first time the code is executed, the url http://repo1.maven.org/maven2/.pack.gz is requested resulting in a 404. Only than the original url is requested resulting in a 304 NOT MODIFIED. All subsequent executions don't involve any communication to the server.
I think the approach of transparently enhancing the urlconnection with caching capabilities is nice and fine and helps improve performance of client applications. But since the server in this case didn't define an Expires header nor a cache-control header, I think the code above should always ask the server and not silently ignore my request.
Case 2
Following code does not work when executed with webstart 10.1.1.255 (this was installed by some early beta Version of java 7, but I don't know which one this was)
URL url = new URL("http://repo1.maven.org/maven2/");
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.connect();
InputStream is = connection.getInputStream();
if ("gzip".equalsIgnoreCase(connection.getContentEncoding()))
{
is = new GZIPInputStream(is);
}
is.close();
With Java Web Start 10.1.1.255 starting with the second execution I got a
java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
With both Java Web Start 1.6.0_24 and now Java Web Start 10.2.1.255 I am not able to reproduce the problem.
With Wireshark I saw that in the case where I got the error, the http header contained an If-Modified-Since entry, and the return code therefore was 304. In the other cases there was no If-Modified-Since. Therefore I think that caching is not active in the stable versions of webstart -- despite the last sentence of the above link.
It seems, that the cache of the beta version does aggressive tuning to http get requests: It does use If-Modified-Since and automatically tries to use gzip encoding -- even if the client code does not set this header. But when the cache is hit, the returned stream is not gzipped, although getContentEncoding returns "gzip".
Since the caching seems not to be active in the stable version of webstart on my machine, I cannot verify if the bug is in the code any more and therefore hesitate to file a bug report.
The only information I have found so far is at Java Rich Internet Applications Enhancements in JDK 7
Caching enabled by default: Caching of network content for application code running in Web Start mode is now enabled by default. This allows application improved performance and consistency with applet execution mode. To ensure the latest copy of content is used, the application can use URLConnection.setUseCaches(false) or request header Cache-Control values no-cache/no-store.
[...]
Improvements for handling content with gzip encoding: The deployment cache will keep application content in compressed form and return it to the application as-is with gzip content-encoding in the HTTP header. This makes behavior more consistent across different execution modes (first launch versus subsequent launch, cache enabled versus cache disabled). See 6575586 for more details.
I modified your code. Hope it works for you.
URL url = new URL("http://repo1.maven.org/maven2/");
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Encoding", "ISO-8859-1");
connection.connect();
InputStream is = connection.getInputStream();
if ("gzip".equalsIgnoreCase(connection.getContentEncoding()))
{
is = new GZIPInputStream(is);
}
is.close();
The cache appears to be implemented by com.sun.deploy.cache.DeployCacheHandler, which lives in deploy.jar. I can't find the source in any official repositories; that link is to some sort of grey-market copy.
I can't, at a glance, find any indications that it is disabled (or enabled!) on any particular platforms. This cache handler has been present since at least Java 6.
It only caches GET requests. A comment in the isResourceCacheable method explains:
// do not cache resource if:
// 1. cache disabled
// 2. useCaches is set to false and resource is non jar/zip file
// 3. connection is not a GET request
// 4. cache-control header is set to no-store
// 5. lastModified and expiration not set
// 6. resource is a partial body resource
I don't see any way to directly configure the cache.

how to test proxy internet connection using java?

i have some code to test if the proxy server and port is working ,some of the code like this:
System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "localhost");
System.getProperties().put("https.proxyPort", "1234");
System.getProperties().put("http.proxyHost", "localhost");
System.getProperties().put("http.proxyPort", "1234");
HttpURLConnection conn = (HttpURLConnection) new URL("https://www.google.com").openConnection();
conn.getContent();
conn.disconnect();
it seems that openConnection() method will do thing like this:
try to connect given URL using proxy.
if it fails to use proxy,it will connect URL directly without proxy.
that's the problem,i meant to test if the proxy is working,but this code won't stop if the proxy can not connect.
i also tried to use isReachable() method of InetAddress class,but i get the same result.
so how could i stop this connection if the proxy doesn't work ,in order to test if the proxy is reachable ?
System.getProperties().put("proxySet", "true");
That one doesn't do anything. It is an urban myth. It was part of the defunct 1997 HotJava bean and leaked from there into various books. It has never been part of any JDK. Try setting it to false in some situation where you need it on and see for yourself.
Sorry guys, I found out the way to do it.
I used java.net.Proxy class to open a connection via proxy.
It's easy to use and works fine. See Java Networking and Proxies

Categories