open url connection and wait to finish loading of the url java - java

i have the following code. i want to get the url after finished loading the url. How can i do my connection to wait to finish the loading first?
Thank you in advance
URL u = new URL("http://google.com/search?sourceid=navclient&btnI=1&q=" + jTextField1.getText().toString());
HttpURLConnection con = (HttpURLConnection) u.openConnection();

HttpURLConnection ex1 = (HttpURLConnection) u.openConnection();
ex1.setChunkedStreamingMode(7000); // enable chunked streaming mode before connecting to server.

Related

When HTTPConnection is performed and what is its status

I have below code which generate OutputStream from HttpURLConnection
When connection is actually performed and how can I check its status?
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.addRequestProperty("X-Auth-Token", getAuthToken());
httpCon.setDoInput(true);
httpCon.setRequestProperty("Connection", "close");
httpCon.setReadTimeout(READ_TIMEOUT);
httpCon.setRequestProperty("Transfer-Encoding","chunked");
httpCon.setDoOutput(true);
httpCon.setChunkedStreamingMode(STREAMING_CHUNK);
mOutputStream = httpCon.getOutputStream();
The underlying TCP connection is created (or allocated from a connection pool) when you get one of the streams or the response code, or call connect(). The HttpURLConnection object itself isn't a TCP connection.

Android setRequestProperty in a url.openConnection()

I have an Android app that need to set a requestproperty in a connection. Here is my code:
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("cookie", cookievalue);
connection.connect();
When I call the setRequestProperty method it launch the exception:
java.lang.IllegalStateException: Cannot set request property after connection is made
Is there a way to create the connection to the file without using the url.openConnection() ?
Here url.openCOnnection() will open new connection to the resource referred to by this URL.
Here you again opening a connection by calling url.connect() method. So remove that
Check this.. for the sample example...
You could try to use the CookieManager mentioned in http://developer.android.com/reference/java/net/HttpURLConnection.html
Set your cookie to CookieManager
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
HttpCookie cookie = new HttpCookie("lang", "fr");
cookie.setDomain("twitter.com");
cookie.setPath("/");
cookie.setVersion(0);
cookieManager.getCookieStore().add(new URI("http://twitter.com/"), cookie);
Source: http://developer.android.com/reference/java/net/HttpURLConnection.html
Use url.openConnection() after you set your cookie.

How to handle multiple requests using HttpURLConnection in a MVC app?

I have a MVC application and I'd like that each request must handle its own session.
My request:
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
As we know CookieHandler allow just one session per application. I'd like that each request creates and use its own session.
Does anybody know how to do it?
Thanks
you will have to use Proxy while opening a connection...
the use of proxy provides always a new IP address for the server so you can be sure that the server maintains different session for each request...
your code will be something like following...
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy("some_proxy"));

how to set tcp.nodelay when i use httpurlconnection

How to set tcp.nodelay for below given code:
URL url = new URL(urlText);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
As far as I am aware you cannot set tcp.noDelay on `HttpURLConnection' as this does not allow any interface to alter underlaying tcp socket.
What I can recommend is try using Apache http client as it provide mechanism to set multiple TCP options. Have a look at this DefaultHttpClient

Blocking operations in URLConnection

Running the following code snippet:
URL url = new URL(uri);
URLConnection urlconn = url.openConnection();
urlconn.connect();
urlconn.getContentLength();
I get the dead-lock sometimes, usually at two lines: urlconn.connect() and urlconn.getContentLength(). Also, when I do
InputStream is = urlconn.getInputStream();
// doing some reading
is.close();
I also get blocking at is.close(). I want to ask why such blocking happens. It is clueless to me from the Java API.

Categories

Resources