httpurlconnection

Cannot write output after reading input

自古美人都是妖i 提交于 2019-11-26 09:53:36
问题 I\'m writing a program that connects to a servlet thanks to a HttpURLConnection but I stuck while checking the url public void connect (String method) throws Exception { server = (HttpURLConnection) url.openConnection (); server.setDoInput (true); server.setDoOutput (true); server.setUseCaches (false); server.setRequestMethod (method); server.setRequestProperty (\"Content-Type\", \"application / xml\"); server.connect (); /*if (server.getResponseCode () == 200) { System.out.println (\

Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager)

◇◆丶佛笑我妖孽 提交于 2019-11-26 08:47:47
问题 I\'ve seen answers about how this should work with the old DefaultHttpClient but there\'s not a good example for HttpURLConnection I\'m using HttpURLConnection to make requests to a web application. At the start of the my Android application, I use CookieHandler.setDefault(new CookieManager()) to automatically deal with the session cookies, and this is working fine. At some point after the login, I want to show live pages from the web application to the user with a WebView instead of

URLConnection getContentLength() is returning a negative value

依然范特西╮ 提交于 2019-11-26 08:32:44
问题 Here is my code: url = paths[0]; HttpURLConnection connection = (HttpURLConnection) url.openConnection(); int length = connection.getContentLength(); // i get negetive length InputStream is = (InputStream) url.getContent(); byte[] imageData = new byte[length]; int buffersize = (int) Math.ceil(length / (double) 100); int downloaded = 0; int read; while (downloaded < length) { if (length < buffersize) { read = is.read(imageData, downloaded, length); } else if ((length - downloaded) <=

How to handle HTTP authentication using HttpURLConnection?

依然范特西╮ 提交于 2019-11-26 08:18:27
问题 I\'m writing a Java client that POSTs to a HTTP server that requires authentication . I have to support at least the following three authentication methods: Basic, Digest or Negotiate. Additionally the POST may be very large (over 2MB), so I need to use streaming. As is documented for HttpURLConnection When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection

How to enable wire logging for a java HttpURLConnection traffic?

♀尐吖头ヾ 提交于 2019-11-26 08:17:35
问题 I\'ve used Jakarta commons HttpClient in another project and I would like the same wire logging output but using the \"standard\" HttpUrlConnection. I\'ve used Fiddler as a proxy but I would like to log the traffic directly from java. Capturing what goes by the connection input and output streams is not enough because the HTTP headers are written and consumed by the HttpUrlConnection class, so I will not be able to log the headers. 回答1: According to Sun's HttpURLConnection source there is

HttpEntity is deprecated on Android now, what&#39;s the alternative?

限于喜欢 提交于 2019-11-26 08:10:29
问题 With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. Looking at the documentation is useless; they all say This class was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details. Which is fine the first time you read it, but when EVERY class that was deprecated says that, it\'s not that helpful. Anyway, what are the alternatives for classes like HttpEntity , specifically StringEntity , and

HttpURLConnection.getResponseCode() returns -1 on second invocation

折月煮酒 提交于 2019-11-26 08:06:36
问题 I seem to be running into a peculiar problem on Android 1.5 when a library I\'m using (signpost 1.1-SNAPSHOT), makes two consecutive connections to a remote server. The second connection always fails with a HttpURLConnection.getResponseCode() of -1 Here\'s a testcase that exposes the problem: // BROKEN public void testDefaultOAuthConsumerAndroidBug() throws Exception { for (int i = 0; i < 2; ++i) { final HttpURLConnection c = (HttpURLConnection) new URL(\"https://api.tripit.com/oauth/request

HttpURLConnection Invalid HTTP method: PATCH

回眸只為那壹抹淺笑 提交于 2019-11-26 07:36:08
问题 When I try to use a non-standard HTTP Method like PATCH with URLConnection: HttpURLConnection conn = (HttpURLConnection) new URL(\"http://example.com\").openConnection(); conn.setRequestMethod(\"PATCH\"); I get an exception: java.net.ProtocolException: Invalid HTTP method: PATCH at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440) Using a higher level API like Jersey generates the same error. Is there a workaround to issue a PATCH HTTP request? 回答1: Yes there is

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found

一个人想着一个人 提交于 2019-11-26 07:29:07
问题 I have some typical codes which used HttpURLConnection to get a file with an URL. They worked fine in android 1.x and 2.x. But failed in Android 4.1! I searched on the web but found little similar information. Would anybody please help to investigate this issue? private String mURLStr; private HttpURLConnection mHttpConnection; ... url = new URL(mURLStr); ... mHttpConnection = (HttpURLConnection) url.openConnection(); mHttpConnection.setDoOutput(true); mHttpConnection.setRequestMethod(\"GET\"

Upload large file in Android without outofmemory error

两盒软妹~` 提交于 2019-11-26 07:24:25
My upload code as below: String end = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { URL url = new URL(ActionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Accept", "text/*"); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream ds = new DataOutputStream(con.getOutputStream()); ds.writeBytes(twoHyphens + boundary + end); ds