how to set tcp.nodelay when i use httpurlconnection - java

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

Related

Https Website using HttpURLConnection

I have a question regarding using an HttpUrlConnection with a https web service.
Basically, we had previously written a bunch of web services to be used in an Android application that were all http calls. We want to change those over to use https instead. Basically, what I'm concerned with, is will my existing code work properly? I had previously created a connection like the following:
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
myConnection.connect();
This appears to be working fine with the new https web services, but I'm wondering why I don't need to change it to something like this:
HttpsURLConnection myConnection = (HttpsURLConnection) myURL.openConnection();
myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
myConnection.setSSLSocketFactory(createSSLSocketFactory());
myConnection.connect();
I'm still planning on moving to a HttpsURLConnection instead, but I'm curious how older versions of our app will be affected by our intended changes.
Thanks for the help!
In docs for HttpUrlConnection you will find:
Calling openConnection() on a URL with the "https" scheme will return
an HttpsURLConnection
But since HttpsUrlConnection extends from HttpUrlConnection, this code:
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
will NOT result in class cast exception if url is for https connection. You still can cast to HttpsUrlConnection - just for safety you can check with instanceof if url scheme should change.

HTTP OPTIONS method with Java

I need to create a simple HTTP client program with Java.
I've not found any example of implementation in Java that allows calling the OPTIONS method to get the Allow header with the allowed methods on the server.
I tried using:
HttpURLConnection http = (HttpURLConnection) url.openConnection();
System.out.println(http.getHeaderFields());
But the field Allow: GET, POST ... is not included.
The connection object fires a GET request by default. You need to set the request method to OPTIONS.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getRequestMethod()); // GET
conn.setRequestMethod("OPTIONS");
System.out.println(conn.getHeaderField("Allow")); // depends

Java Jodd Http Client with proxy

I used Jodd Http library to connect with the proxy:
ProxyInfo proxyInfoObj = new ProxyInfo(ProxyType.HTTP, "10.30.56.70", 8080, "", "");
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
provider.useProxy(proxyInfoObj);
HttpRequest request = HttpRequest.get(url);
request.method("GET");
request.charset("UTF-8");
HttpResponse response = request.open(provider).send();
result = response.bodyText();
But i got this error:
jodd.http.HttpException: HTTP: Invalid code
at jodd.http.net.HTTPProxySocketFactory.createHttpProxySocket(HTTPProxySocketFactory.java:113)
at jodd.http.net.HTTPProxySocketFactory.createSocket(HTTPProxySocketFactory.java:32)
If I use SOCKS4 type, the program hang and don't return anything. Can anyone help me?
But I can connect via proxy using following code:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.30.56.70", 8080));
HttpURLConnection connection =(HttpURLConnection)new URL("http://tvl.csmtalk.vn/api/sms/receive").openConnection(proxy);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-type", "text/xml");
connection.setRequestProperty("Accept", "text/xml, application/xml");
connection.setRequestMethod("GET");
connection.connect();
For me both codes hangs. When I try Jodd, it hangs because it can not open proxy socket to 10.30.56.70:8080. When I try to
telnet 10.30.56.70 8080
from command line it hangs as well. It looks like proxy is not responding. (You can contact Jodd support if you need more details, or if you want to send some private data regarding the connectivity.)
btw, you don't need to:
request.method("GET");
request.charset("UTF-8");
as method is already set to GET by method get() and charset is not used for requests, but response (to set one if not set by server).

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 change the default HTTP OPTIONS parameters in Java

My java snippet looks like:
...
String type = "text/plain;charset=UTF-8";
URL url = new URL("http://xxx/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("OPTIONS");
conn.setRequestProperty("Content-Type", type);
...
When I sniff what this sends it sends a
OPTIONS / HTTP/1.1
which appears to be the default.
However, I actually want to send
OPTIONS * HTTP/1.0
How would I do this?
You can't do that with "plain" java.net.URLConnection. Consider replacing by Apache Commons HttpClient which is less bloated and more configureable. You can force HTTP 1.0 mode by setting http.protocol.version to HttpVersion.HTTP_1_0 in HttpClient#getParams(). You can find an example in this document.
I agree with the answer the following is the code using HTTPClient
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
Hope it helps some one..

Categories

Resources