httpurlconnection

Java, HttpURLConnection and setting the content length

一笑奈何 提交于 2019-12-04 00:19:27
I'm setting the length of the content in my HttpURLConnection, for a PUT. urlConnection.setRequestProperty("Content-Length", "" + responseJSONArray.toString(2).getBytes("UTF8").length); The actual number of bytes is 74. However, when I query the content length of urlConnection I'm returned -1 . Why is that? And why are lengths not equal (given that I set this)? I must set the content-length because I'm receiving a 411 response from the server. (Also, in the Sun examples I've seen the second argument of setRequestProperty is of type int and not String , which seems odd.) You shouldn't set this

Java - HttpUrlConnection returns cached response every time

左心房为你撑大大i 提交于 2019-12-03 23:18:41
I'm trying to gather statistical data from Roblox's currency exchange for analysis. Therefore, I need up-to-date data instead of a cached result. However, it seems that no matter what I do, the result is still cached. It seems that the most intuitive option, setUseCaches() , had no effect, and setting the header manually as Cache-Control: no-cache does not seem to work either. I inspected the Cache header using Fiddler2 and saw that its value was Cache-Control: max-age=0 , but it didn't seem to change the program's behavior either. Here are the relevant pieces of code: URL: private final

Maintain session between HttpUrlConnection Calls (Native/Webview)

冷暖自知 提交于 2019-12-03 22:55:51
Let me start with what I desire : I want to make an app which is part native and part webviews . Problem - Maintain a session between native and webview parts. My Approach to handle this: I intend to implement a native login, in which I present the user with two EditTextboxes and a button, the user enters credentials and I post them as JSON to the server. The Server responds with success or false. Based on Success flag I read the header values for this connection and extract the SessionCookie: switch (responseCode) { case 200: BufferedReader in = new BufferedReader(new InputStreamReader(conn

Retrieve the final location of a given URL in Java

ε祈祈猫儿з 提交于 2019-12-03 21:03:42
问题 I am trying to retrieve the final location of a given URL (String ref) as follows: HttpURLConnection con = (HttpURLConnection)new URL(ref).openConnection(); con.setInstanceFollowRedirects(true); con.setRequestProperty("User-Agent",""); int responseCode = con.getResponseCode(); return con.getURL().toString(); It works in most cases, but rarely returns a URL which yet contains another redirection. What am I doing wrong here? Why do I get responseCode = 3xx, even after calling

Uploading Image byte array with httpurlconnection and android

允我心安 提交于 2019-12-03 20:59:43
I am developing small android application in which I wanted to upload image from my android device to my server. I am using HttpURLConnection for that. I am doing this in following way: Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow_down_float); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, bos); byte[] data = bos.toByteArray(); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "image/jpeg"); connection.setRequestMethod

Can I globally set the timeout of HTTP connections?

会有一股神秘感。 提交于 2019-12-03 15:26:13
问题 I have a program that uses javax.xml.ws.Service to call a remote service defined by a WSDL. This program runs on the Google App Engine which, by default, sets the HTTP connection timeout to 5 seconds{1}. I need to increase this timeout value since this service often takes a long time to respond, but since this request is not being made with URLConnection , I cannot figure out how to call URLConnection.setReadTimeout(int) {2}, or otherwise change the timeout. Is there any way to globally set

Add Header Parameter in Retrofit

假装没事ソ 提交于 2019-12-03 14:49:12
问题 I'm trying to call an API which requires me to pass in an API key. My Service call using HttpURLConnection is working perfectly. url = new URL("https://developers.zomato.com/api/v2.1/search?entity_id=3&entity_type=city&q=" + params[0]); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("user-key","9900a9720d31dfd5fdb4352700c"); if (urlConnection.getResponseCode() != 200) { Toast.makeText(con, "url connection response not 200 | " + urlConnection

Difference between URL.openConnection() and URLConnection.connect()?

断了今生、忘了曾经 提交于 2019-12-03 14:18:38
In the code: HttpURLConnection connection = (HttpURLConnection)createMerchURL.openConnection(); connection.setRequestMethod("PUT"); connection.addRequestProperty("Name", "Value1"); connection.connect(); .. connection.disconnect(); When is the connection getting opened actually? At ..createMerchURL.openConnection(); ? or at connection.connect(); ? How can I set URL in the connection Object and use it with connection.connect() ?(as I am less comfortable with .openConnection() ) Finally, is there any difference between the two? Thanks.. When is the connection getting opened actually? At .

HttpsURLConnection and intermittent connections

二次信任 提交于 2019-12-03 13:31:29
问题 I'm hoping someone could help me out with intermittent connections I'm getting using code with HttpsURLConnection. The code I'm using is below: HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setReadTimeout(10 * 1000); if conn.getResponseCode() != 200) { Log.v(TAG, "error code:" + conn.getResponseCode()); } The connection works the first time everytime when I use it to pull a json file. However, when I use the connection again to send a command, it always fails the

HttpUrlConnection.getInputStream returns empty stream in Android

若如初见. 提交于 2019-12-03 13:06:46
I make a GET request to a server using HttpUrlConnection. After connecting: I get response code: 200 I get response message: OK I get input stream, no exception thrown but: in a standalone program I get the body of the response, as expected: {"name":"my name","birthday":"01/01/1970","id":"100002215110084"} in a android activity, the stream is empty (available() == 0), and thus I can't get any text out. Any hint or trail to follow? Thanks. EDIT: here it is the code Please note: I use import java.net.HttpURLConnection; This is the standard http Java library. I don't want to use any other