httpurlconnection

How to get 401 response without handling it using try/catch in android

做~自己de王妃 提交于 2019-12-03 12:30:02
问题 I am using HttpUrlConnection to make network requests from my android application. Everything works fine except one thing, 401. Whenever the server returns response with status code 401, my app throws IOException with a message stating, "no authentication challenge found" . After googling it, I haven't found a single solution, but only workaround (handling it using try/catch, assuming its a 401 response). here is the code snippet: public Bundle request(String action, Bundle params, String

HttpURLConnection - “https://” vs. “http://”

自作多情 提交于 2019-12-03 12:23:42
问题 I'm trying to get the favicon of the url the user enters, for example _url = "google.com"; I use HttpUrlConnection to get the Bitmap of the favicon from the /favicon.ico extension from the host url. String faviconString = Uri.parse(_url).getHost() + "/favicon.ico"; URL faviconUrl = null; Bitmap favicon = null; try { faviconString = "http://" + faviconString; faviconUrl = new URL(faviconString); HttpURLConnection connection = (HttpURLConnection) faviconUrl.openConnection(); connection

How to stream a JSON object to a HttpURLConnection POST request

萝らか妹 提交于 2019-12-03 11:57:53
问题 I can not see what is wrong with this code: JSONObject msg; //passed in as a parameter to this method HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setDoInput(true); httpCon.setUseCaches(false); httpCon.setRequestProperty( "Content-Type", "application/json" ); httpCon.setRequestProperty("Accept", "application/json"); httpCon.setRequestMethod("POST"); OutputStream os = httpCon.getOutputStream(); OutputStreamWriter osw = new

Is there any way to get upload progress correctly with HttpUrlConncetion

旧巷老猫 提交于 2019-12-03 11:37:57
Android Developers Blog recommend to use HttpURLConnection other than apache's HttpClient ( http://android-developers.blogspot.com/2011/09/androids-http-clients.html ). I take the advice and get problem in reporting file upload progress. my code to grab progress is like this: try { out = conncetion.getOutputStream(); in = new BufferedInputStream(fin); byte[] buffer = new byte[MAX_BUFFER_SIZE]; int r; while ((r = in.read(buffer)) != -1) { out.write(buffer, 0, r); bytes += r; if (null != mListener) { long now = System.currentTimeMillis(); if (now - lastTime >= mListener.getProgressInterval()) {

How to save the file from HTTPS url in JAVA?

瘦欲@ 提交于 2019-12-03 10:20:57
问题 I am trying to save a file from URL using outputstream. The URL is secure by https. So I got some error when I try to get the file as the following javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun

How to get cookies in HttpUrlConnection by using CookieStore?

妖精的绣舞 提交于 2019-12-03 10:08:57
问题 In my Application class, I do the following: public class MyApplication extends Application { private static HttpURLConnection conn = null; public static CookieStore cookieStore; public static HttpContext localContext; public static DefaultHttpClient client = new DefaultHttpClient(); @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); CookieSyncManager.createInstance(this); cookieStore = new BasicCookieStore(); localContext = new BasicHttpContext();

Is it possible to download files like PDF with HttpClient?

吃可爱长大的小学妹 提交于 2019-12-03 09:55:47
问题 I found some examples here on how to download a file but most of them seem to be using HttpURLConnection. is it possible to download files with HttpClient? 回答1: Using httpclient is pretty easy. Here's a link to it's tutorial. http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43 HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(urltofetch); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity();

Java Webstart and URLConnection caching API

耗尽温柔 提交于 2019-12-03 09:30:58
The description of the URLConnection caching API states as the last sentence: There is no default implementation of URLConnection caching in the Java 2 Standard Edition. However, Java Plugin and Java WebStart do provide one out of the box. Where can I find more information about the Webstart ResponseCache? Which Versions of Webstart on which platforms activate Caching? In which cases is it active? Only HTTP Get? Can it be configured? Is the sourcecode available? Background: Case 1 With following (groovy) code def url = new URL('http://repo1.maven.org/maven2/') def connection = url

HttpURLConnection fail on Android

為{幸葍}努か 提交于 2019-12-03 09:00:54
问题 ( Solved - see comment below ) I have a class that implements a multipart file upload. The code works on every java client I've tried it on except for Android, and it's the only HTTP request code in my Android app that doesn't play nice with my back end service. The connection responseCode is "-1" so something pretty nasty is going on here. No entries show up in the Apache access or error logs, it seems as if the request is never making it off of the android platform. The code gets right

Persistent HttpURLConnections on Android

最后都变了- 提交于 2019-12-03 08:57:12
I've got an issue trying to get the Android application (well, Service, it case it makes any difference) to use persistent HTTP 1.1 connections. The following loop (simplified test case) works through a single TCP session on a desktop JRE, but on an Android device results in the whole socket creation/teardown cycle. while (true) { URL url; try { url = new URL("http://10.0.0.125:8080/SRV?"); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); } catch (MalformedURLException e) { }