We have next code.
Sometimes we should wait 10-20-40 seconds on the last line.
What can be the problem?
Java 1.4
URL url = ...;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
OutputStream out = conn.getOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(out);
try
{
outStream.writeObject(objArray);
}
finally
{
outStream.close();
}
InputStream input = conn.getInputStream();
UPDATED:
Next code fixes the problem IN ECLIPSE.
But it still DOES NOT WORK via Java WebStart:(
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
System.setProperty("http.keepAlive", "false"); //<---------------
conn.connect();
But why?
UPDATED one more time!
Bug was fixed! :)
We worked with connections not in one class but in two.
And there is following line in the second class:
URL url = ...
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Length", "1000"); //<------------
conn.connect();
Note:
setRequestProperty("Content-Length", "1000") is root cause of the problem.
'We had a similar issue which is caused by buggy keep-alive in old Java. Add this before connect to see if it helps,
conn.setRequestProperty("Connection", "close");
or
System.setProperty("http.keepAlive", "false");
Had the same problem, found out it was caused by IPv6.
You Disable it from code using:
System.setProperty("java.net.preferIPv4Stack" , "true");
You can also disable it via the command line using : g-Djava.net.preferIPv4Stack=true
Try it with an IP address. To see if it's a DNS problem.
I had same problem, so i change to HTTPClient from Apache, follow a example:
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("www.myurl-to-read");
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(8000)
.setConnectTimeout(10000)
.setConnectionRequestTimeout(1000)
.build();
request.setConfig(requestConfig);
request.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
The problem can be something from network sub layer... Should be hard to find it.
But what about the setReadTimeOut() with low value and a while loop?
One thing I would guess is that your DNS server isn't responding well.
Can you experiment with changing symbolic domain names to numeric IP addresses before you start? Or can you do each request twice (just for experimentation) and see if the first request is significantly slower than the second?
Google has put up a DNS server at (among others) 8.8.8.8 . They claim it's faster than most other DNS servers. Give that a try!
Related
I am trying to do a http PATCH request but I always get the 404 error, so maybe the settings of my connection are not correct:
URL url = new URL("MyPath");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
JsonObject jo = createMyJson();
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jo.toString());
out.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
I get the 404 error, Not found. When doing the same request using Postman, this is working..
Thank you for your help.
Not all servers support X-HTTP-Method-Override. In that case your last resort is (if you are not using a decent HTTP client) to hack the URLConnection object.
I posted a complete solution here on SO, check it out.
I try to send a request from an Android device to a Nodejs server using
HttpURLConnection con = (HttpURLConnection) (new URL(IP + "/getrestaurant")).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
con.getOutputStream().write("{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}".getBytes("UTF-8"));
con.getOutputStream().flush();
con.getOutputStream().close();
When I print the received request from node js I get:
{
"{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}": ""
}
instead of {'restaurant_id':'569a16e28dcdc5c8add2a8e0'}
How can I get it work? Thanks!
as the comment says(i have too low reputation to comment), {'restaurant_id':'569a16e28dcdc5c8add2a8e0'} is not valid context
also I advice you to use Koush Ion library for json requests . https://github.com/koush/ion
It is very convenient and easy to use for , give it a try . In one line all types of requests are done ,
The problem was that I needed con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
I am trying to set Cookies and request headers via URL connection. Here is the client side code
UUID = request.getHeader("UUID");
conn.addRequestProperty("Cookie", iPlanetDirectoryPro);
conn = url.openConnection();
conn.setDoOutput(true);
objOstr = new OutputStreamWriter(conn.getOutputStream());
objOstr.write(res);
One server side i am trying a retrieve the cookie using "iPlanetDirectoryPro" as the cookie name. But i am getting null. What is the mistake i am doing here?
If i set as conn.addRequestProperty("iPlanetDirectoryPro", iPlanetDirectoryPro); then what is the difference between cookie-and-string-in-request-header https://stackoverflow.com/questions/21226475/difference-between-cookie-and-string-in-request-header
Try using:
conn.getRequestProperty("Cookies");
See the doc.
I was trying to get a certain page through java, but with this page I didn't succeed.
Now in my browser it does work, but when I disable Cookies in the settings, it doesn't anymore.
So I probably need to add cookies to my post request in java.
So I went searching the interwebs, but unfortunately I couldn't really find anything useful. mostly it was vague, scattered or irrelevant.
So now my question :
Could anyone show me how to do it (mentioned above^^), or point me to a clear site?
Here's a simple example of setting a cookie in a POST request with URLConnection:
URL url = new URL("http://example.com/");
String postData = "foo bar baz";
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Cookie", "name=value");
con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
con.connect();
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
out.write(postData);
out.close();
You probably need to pass a cookie from a previous request, see this answer for an example. Also consider using Apache HttpClient to make things easier.
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
conn.connect();
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..