httpurlconnection

TCP connection is not reused for HTTP requests with HttpURLConnection

泪湿孤枕 提交于 2019-11-27 18:50:49
问题 I'm have created an application which sends GET requests to a URL, and then downloads the full content of that page. The client sends a GET to e.g. stackoverflow.com, and forwards the response to a parser, which has the resposibility to find all the sources from the page that needs to be downloaded with subsequent GET requests. The method below is used to send those GET requests. It is called many times consecutively, with the URLs returned by the parser. Most of those URLs are located on the

Using browser's certificate in java program

ε祈祈猫儿з 提交于 2019-11-27 18:39:02
问题 I am trying to make HTTP GET request using HttpURLConnection in java. When I make get using browser it says me certificate is not trusted do you want to proceed. I accept certificate and GET request get data. but i am getting certificate exception in java( given below ) What i understood from this exception is, I need to download that certificate and put this java system property berfore making GET request. My questions are. How will download this certificate from browser? Can I use browser's

Getting a reference to Java's default http(s) URLStreamHandler

蹲街弑〆低调 提交于 2019-11-27 18:36:10
问题 I have a library that I need to use in one of my projects, which unfortunately registers its own URLStreamHandler to handle http- URLs . Is there a way to get a reference to Java's default http- and https- URLStreamHandlers , so I can specify one of them in the URL 's constructor to open a standard http connection without using the protocol overridden by the library? 回答1: Found it: sun.net.www.protocol.http.Handler With that, I can now do: URL url = new URL(null, "http://...", new sun.net.www

How to check if InputStream is Gzipped?

被刻印的时光 ゝ 提交于 2019-11-27 18:18:40
Is there any way to check if InputStream has been gzipped? Here's the code: public static InputStream decompressStream(InputStream input) { try { GZIPInputStream gs = new GZIPInputStream(input); return gs; } catch (IOException e) { logger.info("Input stream not in the GZIP format, using standard format"); return input; } } I tried this way but it doesn't work as expected - values read from the stream are invalid. EDIT: Added the method I use to compress data: public static byte[] compress(byte[] content) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { GZIPOutputStream gs =

Two way sync for cookies between HttpURLConnection (java.net.CookieManager) and WebView (android.webkit.CookieManager)

天涯浪子 提交于 2019-11-27 17:27:24
Unfortunately, there's a multitude of cookie managers for Android. The cookies for HttpURLConnection are maintained by java.net.CookieManager and the cookies for WebView are maintained by android.webkit.CookieManager . These cookie repositories are separate and require manual synchronization. My app uses both HttpURLConnections and shows WebViews (it's a native-HTML hybrid). Naturally, I want both to share all cookies - so I will have a transparent session all across. More Specifically: When a cookie is set/changed in an HttpURLConnection, I want the WebViews to see this change as well. When a

Why am I getting this error Premature end of file?

拥有回忆 提交于 2019-11-27 17:26:37
问题 I am trying to parse and XML response , but I am failing miserably. I thought initially that the xml was just not being returned in the response, so I crafted up the code below with a direct link to my xml file online. I am able to print the XML to screen with no problems. However when I call my parse method I get Premature end of file. It works if I pass the URL directly: builder.parse(""); but fails when I passed an InputStream: builder.parse(connection.getInputStream()); try { URL url =

How to implement Login with HttpURLConnection and PHP server in Android

萝らか妹 提交于 2019-11-27 17:18:47
问题 I have a Web Service which has login functionality, connecting to a PHP back end: Htp://hfbhgvbbnvhn.com/mypictures/loginService.php (its in fact http) I have to send two parameters to it and in return I would get the JSONObject. But right now I am trying to use the HttpUrlConnection object to connect to the PHP service, but I have no idea to how to send the parameters. I have searched many solutions but they all are working on NameValuePair with DefaultHttpClient where as I think I have to

Java: Display request of an HttpURLConnection before sending

偶尔善良 提交于 2019-11-27 15:44:48
问题 I want to make some API calls to a server using HttpURLConnection . But the requests are not successful, returning: <error> <http_status>400 Bad Request</http_status> <message>Unexpected request Content-Type header ''. Expecting 'application/x-www-form-urlencoded'.</message> </error> So I want to check what the "real" content is that is being sent to the server. By real content I mean the exact HTTP request. Any ideas how I can see this? Edit: Based on the first answers here I should clarify

Digest authentication in Android using HttpURLConnection

女生的网名这么多〃 提交于 2019-11-27 15:18:48
as the question allready says, I am trying to do digest authentication in android. Until now i have used the DefaultHttpClient and it's authentication method (using UsernamePasswordCredentials and so on), but it is deprecated since Android 5 and will be removed in Android 6. So i am about to switch from DefaultHttpClient to HttpUrlConnection . Now i am trying to achieve digest authentication, which should work pretty simple as explained here : Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username

Android - HttpUrlConnection is not closing. Eventually results to SocketException

大兔子大兔子 提交于 2019-11-27 15:18:35
问题 I am encountering some problems with the HttpUrlConnection in devices running Jellybean (4.1 - 4.3) wherein connections are not closed and results to a SocketException "Too many open files" after executing a number of times. I do call HttpUrlConnection.disconnect() and am closing all the Inputstream, Outputstream, Reader and Writers in a finally block. Going to adb shell and executing a netstat shows all the connections created by the application are left in CLOSE_WAIT state. InputStream