httpurlconnection

Setting custom header using HttpURLConnection

别等时光非礼了梦想. 提交于 2019-12-03 07:04:59
I am simply making a GET request to a Rest API using HttpURLConnection . I need to add some custom headers but I am getting null while trying to retrieve their values. Code: URL url; try { url = new URL("http://www.example.com/rest/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Set Headers conn.setRequestProperty("CustomHeader", "someValue"); conn.setRequestProperty("accept", "application/json"); // Output is null here <-------- System.out.println(conn.getHeaderField("CustomHeader")); // Request not successful if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)

httpurlconnection is very slow on Android 4.2

删除回忆录丶 提交于 2019-12-03 06:58:14
I can succesfully connect, send and receive data by using httpurlconnection. But it takes very long time to load all the data on my phone (Samsung s4, 4.2) and on android 4.2 emulator. But it takes almost 1-2 seconds (which is very fast) to load pics on Android 2.3.x emulator. Faster than my galaxy s4 on http connection. I'm using AsyncTask and my code works fine on both. It is just slow on android 4.2s. I tried removing chunkedStreaming, keep alive, changing timeout values etc. but still no success Here is my code HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

Android App Strategy for keeping track of a login session

拟墨画扇 提交于 2019-12-03 05:19:35
问题 I have some PHP script that logs in and returns a JSON array with a session ID if the login was successful. In my app, I want to login at the front page and continue out through the app being logged in. I created a singleton class that holds a session ID (along with a few other fields) received from the JSON from the PHP page. This singleton object's field "session_id" gets checked depending on what the user does. If the user wants to log out, then the session_id just gets set to null thus

Android: How to login into webpage programmatically, using HttpsURLConnection

心已入冬 提交于 2019-12-03 03:50:56
I'm new in Android (and in Java too), so sorry if my problem is a basic proposition! I have to write an Android app, whitch signs into an aspx webpage in the background, get some data from it, and after that logs out form the webpage. (and do that all programmatically) Basicly, the procedure likes getting email-list from Gmail: 1. go to 'https://mail.google.com', and signs in 2. click to the "Contacts" (== go to "https://mail.google.com/mail/?shva=1&zx=dzi4xmuko5nz#contacts") 3. fetch the page using HttpsURLConnection (or something like this), and get emails in an (e.g. Map or String) object 4

How to stream a JSON object to a HttpURLConnection POST request

不问归期 提交于 2019-12-03 03:17:42
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 OutputStreamWriter(os, "UTF-8"); msg.write(osw); osw.flush(); osw.close(); os.close(); //probably overkill On the

详解HttpURLConnection

﹥>﹥吖頭↗ 提交于 2019-12-03 03:04:23
请求响应流程 设置连接参数的方法 setAllowUserInteraction setDoInput setDoOutput setIfModifiedSince setUseCaches setDefaultAllowUserInteraction setDefaultUseCaches 设置请求头或响应头 HTTP请求允许一个key带多个用逗号分开的values,但是HttpURLConnection只提供了单个操作的方法: setRequestProperty(key,value) addRequestProperty(key,value) setRequestProperty和addRequestProperty的区别就是,setRequestProperty会覆盖已经存在的key的所有values,有清零重新赋值的作用。而addRequestProperty则是在原来key的基础上继续添加其他value。 发送URL请求 建立实际连接之后,就是发送请求,把请求参数传到服务器,这就需要使用outputStream把请求参数传给服务器: getOutputStream 获取响应 请求发送成功之后,即可获取响应的状态码,如果成功既可以读取响应中的数据,获取这些数据的方法包括: getContent getHeaderField getInputStream 对于大部分请求来说

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

有些话、适合烂在心里 提交于 2019-12-03 02:53:35
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.setDoInput(true); connection.connect(); favicon = BitmapFactory.decodeStream(connection.getInputStream()); }

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

亡梦爱人 提交于 2019-12-03 01:59:50
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 cookie) throws FileNotFoundException, MalformedURLException, SocketTimeoutException, IOException {

How to save the file from HTTPS url in JAVA?

﹥>﹥吖頭↗ 提交于 2019-12-03 00:52:13
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.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source)

How to get cookies in HttpUrlConnection by using CookieStore?

青春壹個敷衍的年華 提交于 2019-12-03 00:40:18
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(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); } ... } And I have a connection in