httpurlconnection

Android's HttpURLConnection throws EOFException on HEAD requests

一世执手 提交于 2019-11-27 15:01:10
This small code snippet runs fine on my Mac's JVM. Unfortunately it crashes when executed on Android 4.2. import java.net.HttpURLConnection; import java.net.URL; public class App { public static void main( String... arguments ) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL( "https://github.com" ).openConnection(); connection.setRequestMethod( "HEAD" ); System.out.println( connection.getResponseCode() + "" ); } } If I replace https://github.com with https://www.facebook.com it works fine but I'm failing to figure out why. The exception does not contain a message;

URLConnection does not get the charset

无人久伴 提交于 2019-11-27 14:37:24
问题 I'm using URL.openConnection() to download something from a server. The server says Content-Type: text/plain; charset=utf-8 But connection.getContentEncoding() returns null . What up? 回答1: This is documented behaviour as the getContentEncoding() method is specified to return the contents of the Content-Encoding HTTP header, which is not set in your example. You could use the getContentType() method and parse the resulting String on your own, or possibly go for a more advanced HTTP client

How to stop HttpURLConnection connect on Android

送分小仙女□ 提交于 2019-11-27 14:06:20
I use AsyncTask to connect an URLPath as below code. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.left_list); Button btn = (Button)findViewById(resid); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //how to stop connect } }); new Connecting().execute(); } class Connecting extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); //do something } @Override protected String doInBackground(String... aurl) { try { URL url = new URL

Need an example of HttpResponseCache in Android

血红的双手。 提交于 2019-11-27 13:56:37
问题 Hi I am trying to use the HttpResponseCache introduced in Android 4.The docs do talk clearly about how to install the cache but I am at a complete loss on how to cache Images downloaded from the net.Earlier I was using the DiskLruCache to cache them. Would anyone point me towards some examples of working code where HttpResponseCache has been used.. Edit:- Can someone tell me what I am doing wrong here:- MainActivity.java public void onCreate(Bundle savedInstanceState) { super.onCreate

How to read full response from HttpURLConnection?

孤人 提交于 2019-11-27 13:44:54
问题 I make some proxy server in andorid which modify http headers, it works ok, but I have to forward full response to 'top layer'. How I can read whole response (all headers, content, everything) from HttpURLConnection? HttpURLConnection httpURLConnection; URL url = new URL(ADDRESS); httpURLConnection = (HttpURLConnection) url.openConnection(); // add headers, write output stream, flush if (httpURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { Map<String, List<String>> map =

Reusing HttpURLConnection so as to keep session alive

泄露秘密 提交于 2019-11-27 12:42:29
问题 We have an Android application that requires the user to enter an answer to a Captcha. The Captcha is generated on our server. When the replies, it is sent to the server for verifying. Problem is that since I have to close the HttpURLConnection after the request for the Captcha I then find that the reply is running on a different session on the sever. Because of this the Captcha check fails since it is session dependant. Is there a way to keep the connection alive or should I be following a

How to get response body using HttpURLConnection, when code other than 2xx is returned?

人盡茶涼 提交于 2019-11-27 11:44:39
I have problem with retrieving Json response in case when server returns error. See details below. How I perform the request I use java.net.HttpURLConnection . I setup request properties, then I do: conn = (HttpURLConnection) url.openConnection(); After that, when request is successful, I get response Json: br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); sb = new StringBuilder(); String output; while ((output = br.readLine()) != null) { sb.append(output); } return sb.toString(); ... and the problem is: I can't retrieve Json received when the server returns some error

cURL and HttpURLConnection - Post JSON Data

五迷三道 提交于 2019-11-27 11:36:31
How to post JSON data using HttpURLConnection? I am trying this: HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection())); httpcon.setDoOutput(true); httpcon.setRequestProperty("Content-Type", "application/json"); httpcon.setRequestProperty("Accept", "application/json"); httpcon.setRequestMethod("POST"); httpcon.connect(); StringReader reader = new StringReader("{'value': 7.5}"); OutputStream os = httpcon.getOutputStream(); char[] buffer = new char[4096]; int bytes_read; while((bytes_read = reader.read(buffer)) != -1) { os.write(buffer, 0, bytes_read);// I am

How to set Content Type on HttpURLConnection?

一个人想着一个人 提交于 2019-11-27 11:33:26
Do you know how to set Content-Type on HttpURLConnection ? Following code is on Blackberry and I want the Android equivalent: connection.setRequestProperty("content-type", "text/plain; charset=utf-8"); connection.setRequestProperty("Host", "192.168.1.36"); connection.setRequestProperty("Expect", "100-continue"); Is it right for android? Please advise. If you really want to use the HttpURLConnection you can use the setRequestProperty method like: myHttpURLConnection.setRequestProperty("Content-Type", "text/plain; charset=utf-8"); myHttpURLConnection.setRequestProperty("Expect", "100-continue");

HttpURLConnection to Send image , audio and video files with parameter may (String or Json String) Android

送分小仙女□ 提交于 2019-11-27 11:07:26
I'm sharing the solution to send an image , audio or a video file with parameters using HttpURLConnection . Parameters may be (plain string or JSON). (Android Client to PHP Back end) Scenario: Have to upload media files (audio , video and image with parameters). Media files will be stored in a server folder and parameters to db. I faced a problem that, image was uploaded successfully while parameters went missing . Potential solution found Choosing HttpURLConnection instead of Httpclient as recommend here You may be wondering, Which client is best? Apache HTTP client has fewer bugs on Eclair