httpurlconnection

several requests from one HttpURLConnection

*爱你&永不变心* 提交于 2019-11-30 01:32:12
问题 How can I do several request in one HttpURLConnection with Java? URL url = new URL("http://my.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); HttpURLConnection.setFollowRedirects( true ); connection.setDoOutput( true ); connection.setRequestMethod("GET"); PrintStream ps = new PrintStream( connection.getOutputStream() ); ps.print(params); ps.close(); connection.connect(); //TODO: do next request with other url, but in same connection Thanks. 回答1: From the Javadoc

Android (Java) HttpURLConnection silent retry on 'read' timeout

夙愿已清 提交于 2019-11-29 23:21:15
So I'm using Google Volley for HTTP request, which basically uses Java 's HttpURLConnection . According to my tests, the problem is this : When the 'read' timeout on the HttpURLConnection reaches, a silent retry is executed before the connection is closed and the relevant exception thrown ( SocketTimeoutException ). Note that: - I noticed this error when using HTTP POST request. - 'read' timeout is different than 'connect' timeout. - If the 'read' timeout (set by calling connection.setReadTimeout(int) ) is NOT set (0), or set to a greater value than connection.setConnectTimeout(int) , this

To transfer file from pc to android device via usb

≡放荡痞女 提交于 2019-11-29 21:09:17
问题 I want to transfer a file from my pc's particular port to android device over USB programmatically. Which technique is best for this mode of transfer among the following : Sockets HttpUrlConnection. If it is possible through HttpUrlConnection means how to implement this? 回答1: To transfer file from pc to android device via USB Socket Communication is best approach. You have to do some additional step of port forwarding on which the Socket Communication establish. Look at Tutorial https:/

How to send Https Post request in java

*爱你&永不变心* 提交于 2019-11-29 20:37:58
I want to login to application from java code. Here is my code... String httpsURL = "https://www.abcd.com/auth/login/"; String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8"); query += "&"; query += "password="+URLEncoder.encode("abcd","UTF-8") ; URL myurl = new URL(httpsURL); HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-length", String.valueOf(query.length())); con.setRequestProperty("Content-Type","application/x-www- form-urlencoded"); con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;

HttpURLConnection: Is it necessary to call connect()?

邮差的信 提交于 2019-11-29 18:12:19
问题 Many examples I've seen don't explicitly call connect() . Instead they just use getInputStream() or getResponseCode() . I'm assuming all of these HttpURLConnection methods that require a connection just call connect() themselves? Are there any cases where connect() must be explicitly called for an HttpURLConnection? 回答1: No, there are no cases. It's implicitly executed on demand. It's even specified in the documentation. Here's an extract of the URLConnection#connect() javadoc: Operations

Send data in Request body using HttpURLConnection

做~自己de王妃 提交于 2019-11-29 17:10:28
问题 I am using HttpURLConnection to make a POST request to a local service deployed in my local created using JAVA Spark. I want to send some data in request body when I make the POST call using the HttpURLConnection but every time the request body in JAVA Spark is null . Below is the code I am using for this Java Spark POST Service Handler post("/", (req, res) -> { System.out.println("Request Body: " + req.body()); return "Hello!!!!"; }); HTTPClass Making the post call `public class

get body of Bad Request httpURLConnection.getInputStream()

我怕爱的太早我们不能终老 提交于 2019-11-29 16:56:51
问题 I've work on a portlet that use Restful web service to get info. so, when i call webservice and person id was not exist, it return an appropriate error message in json format (with Bad request code - 400), and if person id be valid, return person info in json (with code 200). now, how can i read body of response (that contain error description) because invoking "httpConn.getInputStream()" will throw exception in bad request mode. this part of my code: HttpURLConnection httpConn = null; URL

Posting a large file in Android

十年热恋 提交于 2019-11-29 16:10:30
I sometimes get an OutOfMemoryError when posting a large file in Android. This is the code I'm using. Am I doing something wrong? HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoInput(true); con.setDoOutput(true); ostream = new DataOutputStream(con.getOutputStream()); byte[] buffer = new byte[1536]; int count; while ((count = fileInput.read(buffer)) != -1) { ostream.write(buffer, 0, count); // This sometimes causes OutOfMemoryError } ostream.flush(); Would calling ostream.flush() inside the while loop do any good? Matthew Flaschen If you

Using HttpURLConnection and HttpsURLConnection to connect to an https?

限于喜欢 提交于 2019-11-29 16:09:09
问题 What are the differences if I try to connect to an "https" using HttpURLConnection and HttpsURLConnection ? I was able to connect to "https" using both HttpURLConnection and HttpsURLConnection so I am confused. I thought I can only establish a connection to an "https" if I used HttpsURLConnection? Below are some of my questions: If I was able to open a connection to an "https" using HttpURLConnection, is my connection still in "https" or does it become "http"? How about the validating of

HttpURLConnection sending JSON POST request to Apache/PHP

◇◆丶佛笑我妖孽 提交于 2019-11-29 14:09:32
问题 I'm struggling with HttpURLConnection and OutputStreamWriter. The code actually reaches the server, as I do get a valid error response back. A POST request is made, but no data is received server-side. Any hints to proper usage of this thingy is highly appreciated. The code is in an AsyncTask protected JSONObject doInBackground(Void... params) { try { url = new URL(destination); client = (HttpURLConnection) url.openConnection(); client.setDoOutput(true); client.setDoInput(true); client