java - HttpURLConnection.getInputStream() throws SocketTimeoutException -
i using httpurlconnection
upload image , response.
works on emulator , xiaomi device.
however, sockettimeoutexception
on sony device on line connection.getinputstream()
.
i've tried set timeouts large value 1 minute not work.
public string uploadfile(file file, string requesturl) { if (file != null) { long filesize = file.length(); httpurlconnection.setfollowredirects(false); httpurlconnection connection = null; try { //config of connection connection = (httpurlconnection) new url(requesturl).openconnection(); connection.setconnecttimeout(10000); connection.setreadtimeout(10000); connection.setrequestmethod("put"); connection.setrequestproperty("content-type", "image/jpeg"); connection.setdooutput(true); connection.setrequestproperty("content-length", "" + filesize); connection.connect(); //upload file dataoutputstream out = new dataoutputstream(connection.getoutputstream()); int bytesread; byte buf[] = new byte[1024]; bufferedinputstream bufinput = new bufferedinputstream(new fileinputstream(file)); while ((bytesread = bufinput.read(buf)) != -1) { out.write(buf, 0, bytesread); out.flush(); } out.flush(); out.close(); //get response message, sockettimeoutexception occurs here bufferedreader br = new bufferedreader(new inputstreamreader((connection.getinputstream()))); stringbuilder sb = new stringbuilder(); string output; while ((output = br.readline()) != null) { sb.append(output); } //return response message return output; } catch (exception e) { // exception e.printstacktrace(); } { if (connection != null) connection.disconnect(); } } return null; }
what causes problem happen?and how fix it?
additional info: tested on devices under same wifi connection. , sure network , file server worked properly. file size of tested images 100~200kbyte.
because set read timeout of ten seconds , no response received within ten seconds.
is trick question?
nb don't need set content-length header. java you.
Comments
Post a Comment