httpurlconnection

How to get redirected URL and content using HttpURLConnection

空扰寡人 提交于 2019-11-26 17:02:13
问题 Sometimes my URL will redirect to a new page, so I want to get the URL of the new page. Here is my code: URL url = new URL("http://stackoverflow.com/questions/88326/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setInstanceFollowRedirects(true); System.out.println(conn.getURL().toString()); The output is: stackoverflow.com/questions/88326/does-elmah-handle-caught-exceptions-as-well It works well for the Stack Overflow website, but for the sears.com site, it doesn

Android's HttpURLConnection throws EOFException on HEAD requests

旧街凉风 提交于 2019-11-26 16:59:02
问题 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

Persistent HttpURLConnection in Java

江枫思渺然 提交于 2019-11-26 16:34:42
I am trying to write a java program that will automatically download and name some of my favorite web comics. Since I will be requesting multiple objects from the same domain, I wanted to have a persistent http connection that I could keep open until all the comics have been downloaded. Below is my work-in-progress. How do I make another request from the same domain but different path without opening a new http connection? import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class

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

不问归期 提交于 2019-11-26 15:43:55
问题 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

cURL and HttpURLConnection - Post JSON Data

青春壹個敷衍的年華 提交于 2019-11-26 15:36:19
问题 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

How to set Content Type on HttpURLConnection?

只愿长相守 提交于 2019-11-26 15:34:54
问题 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. 回答1: If you really want to use the HttpURLConnection you can use the setRequestProperty method like: myHttpURLConnection.setRequestProperty(

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

本秂侑毒 提交于 2019-11-26 15:26:27
问题 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

is it possible to proccess JSON responses with the JDK or HttpComponents only?

最后都变了- 提交于 2019-11-26 14:49:27
问题 we are upgrading our web app to use Facebook's Graph API, which returns JSON responses. However we don't want to add dependecy to a JSON library unless we have no other choice. For server-side http requests we use Apache HttpComponents. Thus, my question is what are the classes (if any) in the JDK and/or in HttpComponents that I can use to process JSON responses? Code snippets are welcome :) 回答1: It is possible. Because JSON is valid JavaScript syntax, you can use the built-in JavaScript

Can I override the Host header where using java's HttpUrlConnection class?

梦想与她 提交于 2019-11-26 14:41:50
问题 I'm using the following code to open a http connection in java: URL url = new URL("http://stackoverflow.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("GET"); conn.setRequestProperty("Host", "Test:8080"); conn.getOutputStream(); However calling conn.setRequestProperty("Host", "Test:8080") appears to have no effect regardless of what order I call the methods and the Host is reset to the destination server. Is there any

Android HttpsUrlConnection eofexception

一个人想着一个人 提交于 2019-11-26 13:01:03
问题 I have a problem where my HttpsURLConnection will throw an EOFException when i try to read any input. The code works for some network calls, but fails on others. If i try and read anything from the connection, it fails with the aforementioned error. Example: urlConnect.getResponseCode() // will throw error urlConnect.getResponseMessage() // will throw error BufferedInputStream in = new BufferedInputStream(urlConnect.getInputStream()); //will throw error Here is the stack trace for each: