httpurlconnection

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

寵の児 提交于 2019-11-27 09:37:19
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 :) It is possible. Because JSON is valid JavaScript syntax, you can use the built-in JavaScript interpreter via the scripting API to create and object graph, walk that (using the visitor pattern to push data

Should HttpURLConnection with CookieManager automatically handle session cookies?

二次信任 提交于 2019-11-27 09:32:34
问题 I have a Java application (JDK 1.7.0_13) and am using java.net.HttpURLConnection to connect to some servlet based services that do session management. I am trying to figure out how to use java.net.CookieManager to track session cookies. Reading the docs I get the impression that installing CookieManager with CookieHandler.setDefault(new CookieManager()) should cause cookie management to happen automatically. However, multiple requests to the same URL's does not seem to preserve cookies. Do I

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

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:24:21
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 way to override the Host header without using a different library? TIA Matt Boris This used to work in

HttpURLConnection responsecode is randomly -1

久未见 提交于 2019-11-27 08:05:16
问题 Hi I'm using following code to establish a url connection. But randomly I get the responseCode -1 (which is the default value of responseCode): try { URL url = new URL(urlString); HttpURLConnection httpconn = (HttpURLConnection) url.openConnection(); if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) { handleData(new DataInputStream(httpconn.getInputStream()), requestCode); } else { Log.e(TAG, "HttpConnection not OK: " + httpconn.getResponseCode()); ActivityHelper.httpError(this); }

FileNotFoundException for HttpURLConnection in Ice Cream Sandwich

萝らか妹 提交于 2019-11-27 07:33:04
I have an Android app that works fine with Android 2.x and 3.x, but it fails when run on Android 4.x. The problem is in this section of code: URL url = new URL("http://blahblah.blah/somedata.xml"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); When the application is running on Android 4.x, the getInputStream() call results in a FileNotFoundException . When the same binary is running on earlier versions of

IOException: “Received authentication challenge is null” (Apache Harmony/Android)

情到浓时终转凉″ 提交于 2019-11-27 07:25:06
I am trying to send a GET via Android's HttpURLConnection (imported from org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection ), and upon receiving the response, an IOException is thrown: in doRequestInternal(): "Received authentication challenge is null" What does this error mean, and what is causing this? I am writing OAuth parameters to the Authorization header, but I do this on other occasions, too, without problems. if (connection == null) { connection = (HttpURLConnection) new URL(endpointUrl).openConnection(); connection.setRequestMethod("GET"); } //... do some OAuth

Android FileNotFound Exception - Cannot getInputStream from image URL that does not have file format

佐手、 提交于 2019-11-27 07:24:28
问题 The title is pretty self explanatory. the following code...: URL imageUrl = new URL(url); try { HttpURLConnection conn= (HttpURLConnection)imageUrl.openConnection(); conn.setDoInput(true); conn.connect(); int length = conn.getContentLength(); InputStream is = conn.getInputStream(); return BitmapFactory.decodeStream(is); } catch (IOException e) { e.printStackTrace(); } Will fail if the url does not contain the file format. For example, some images hosted by google will display the image yet

How to switch from HttpClient to HttpUrlConnection?

ⅰ亾dé卋堺 提交于 2019-11-27 07:08:33
问题 I am creating an Android application and I send data from Android application to servlet through HttpClient. I use HttpPost method. I read in Android developer site that Apache HttpClient library has some bug in Android Froyo 2.2 and after all it's good practice to use HttpUrlConnection instead HttpPost. So I want to convert my HttpPost code to HttpUrlConnectio but don't know how. I am posting my Android code as well as servlet code here Android code private String postData(String

HTTP Basic Authentication issue on Android Jelly Bean 4.1 using HttpURLConnection

风流意气都作罢 提交于 2019-11-27 07:04:40
问题 We are making HttpURLConnection based request to the Web server using HTTP Basic Authentication. Code works great on Android versions 2.x, 3.x., 4.0.x Now with Jelly Bean and v4.1.x authentication fails with the following messages in the LogCat: 01-27 10:54:18.886: ...::doReadRawData(731): An exception occured while reading data from remote host. httpURLConn.responseCode = 401 / httpURLConn.responseMessage = UNAUTHORIZED 01-27 10:54:18.886: ...::doReadRawData(731): java.io.IOException: No

How do I persist cookies when using HTTPUrlConnection?

情到浓时终转凉″ 提交于 2019-11-27 06:59:46
I've begun using the recommended HTTPUrlConnection and moved away from the DefaultHTTPClient . One of the things that I haven't been able to glue back together is the use of a persistent cookie store. I'd like to simply attach a custom cookie handler/manager to my connection to store the cookies. The Android documentation hasn't been very helpful as it wraps up the subject about cookies in two lines. I've been using LoopJ's PersistentCookieStore earlier and that worked beautifully. Any idea on how I could set up a persistent cookie store in Android that I can attach to my HTTPUrlConnection