help... me.... please
what is 'Invalid argument (Read failed)'
exception log
java.net.SocketException: Invalid argument (Read failed)
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:127)
at java.net.SocketInputStream.read(SocketInputStream.java:182)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at com.ibm.jsse2.b.a(b.java:262)
at com.ibm.jsse2.b.a(b.java:33)
at com.ibm.jsse2.av.a(av.java:579)
at com.ibm.jsse2.av.i(av.java:574)
at com.ibm.jsse2.av.a(av.java:280)
at com.ibm.jsse2.av.startHandshake(av.java:431)
at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:167)
at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:62)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1346)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1321)
at com.ibm.net.ssl.www2.protocol.https.b.getOutputStream(b.java:53)
at com.my.order.applepay.ApplePayServiceImpl.applePayApproval(ApplePayServiceImpl.java:142)
source code
conn = (HttpURLConnection) url.openConnection();
if (conn != null) {
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
** output = new DataOutputStream(conn.getOutputStream()); // error occurs in here**
output.writeBytes(params);
output.flush();
output.close();
change connectionTimeout to 0,
add setReadTimeout to 0
every time gets same error
Related
I'm trying to connect client server, where I'm facing below mentioned error while opening connection:
java.net.ConnectException: errno: 110 (Connection timed out), error:
Connection timed out (local port XXXXX to address 0:0:0:0:0:0:0:0, remote
port XXXX to address XXX.XXX.XXX.XX)
Code Snippet:
URL url = new URL("http://XXX.XXX.XXX.XX:XXXX/services/ServiceEngine");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(json);
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
Connection timeouts happen when the server fails to respond in a pre-determined time period and shouldn't have much to do with the logic in your code.
You can change this time period with the HttpURLConnection.setConnectTimeout() like this:
connection.setConnectTimeout(10000); //Time is set in milliseconds, so 1000 is 1 second.
Also as another user pointed out it could be that your server isn't actually up and ready to host connections.
This question already has answers here:
Networking code sometimes throws UnknownHostException
(4 answers)
Closed 3 years ago.
URL realUrl = new URL("xxx.xxxx.com");
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
out.print(param);
out.flush();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
InputStream is = conn.getInputStream();
while ((len = is.read(buf)) != -1) {
System.out.println(len);
baos.write(buf, 0, len);
}
Linux OS,Tomcat server,"xxx.xxxx.com" is an incorrect URL.
I am expecting a UnknownHostException all the time, but sometimes what I am getting is UnknownHostException.
UnknownHostException error message
java.net.UnknownHostException: xxx.xxxx.com at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at
java.net.Socket.connect(Socket.java:579) at
sun.net.NetworkClient.doConnect(NetworkClient.java:178) at
sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
SocketTimeoutException error message
java.net.SocketTimeoutException: connect timed out at
java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at
java.net.Socket.connect(Socket.java:579) at
sun.net.NetworkClient.doConnect(NetworkClient.java:178) at
sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
Just a guess - is it possible that sometimes your app can not connect to the DNS server? That would explain why you receive a SocketTimeoutException instead of an UnknownHostException - your app is trying to look up the hostname but it times out before it can determine that the host does not exist.
URL url = new URL("https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=mykey");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(bytearray);
wr.flush();
wr.close();
InputStream stream = conn.getInputStream(); //throws io exception
so basically I'm trying to send bytearray over httpurlconnection java.io.IOException: Error writing to server at last line.
I have some connection to url:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
How I can send post parameter to this url that php function: http://www.php.net/manual/en/reserved.variables.request.php can read it ? I try to according this question: Java - sending HTTP parameters via POST method easily but with no success. It just display empty array
php code:
print_r($_REQUEST,1)
java code:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("category_name", categoryName);
connection.setRequestProperty("complete", complete);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
connection.getResponseCode();
wr.flush();
wr.close();
connection.disconnect();
You forgot to set the Content-Type header, and you need to write the category_name and complete values as a string to the stream.
Quoting the code from the post you mentioned:
String urlParameters = "param1=a¶m2=b¶m3=c";
String request = "http://example.com/index.php";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();
As you can see, he sets the headers (Content-Type, Charset and Content-Length) and then writes the POST data to the stream.
The POST data is just like the GET format: key-value pairs seperated by &, and the key and value are seperated by =.
As the Content-Type is set to www-form-urlencoded, you need to 'url encode' the key and value values. You can do this using the URLEncoder.encode method.
The urlParameters for your POST data would be:
String urlParameters = "category_name=" + URLEncoder.encode(categoryName) + "&complete=" + URLEncoder.encode(complete);
I want to get content size by request.getContentLength() in server client JSP page.
But request.getContentLength() always return -1, i do not why?
Android snippet code:
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
//conn.setChunkedStreamingMode(100);
conn.setConnectTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setReadTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
//conn.setRequestProperty("content-length", "10");
//conn.addRequestProperty("content-length", "20");
conn.setFixedLengthStreamingMode(30);
conn.setRequestProperty("Charsert", ENCODING);
conn.setRequestProperty("Content-Type", "multipart/form-data"
+ ";boundary=" + java.util.UUID.randomUUID().toString());
conn.connect();
You are using conn.setChunkedStreamingMode(100) that will effectively enable the chunked transfer encoding in chunks of 100 bytes when the content-lenght is unknown in advance.
Use conn.setFixedLengthStreamingMode(int len) if you know in advance the length of the content you are going to send in the body of the request.