httpurlconnection

HttpURLConnection c = URL.openConnection(); c.setRequestProperty() Doesn't work

杀马特。学长 韩版系。学妹 提交于 2019-11-30 10:51:33
This Code here is a normal Java application not an android application, this is designed to send C2DM messages to a device with YOUR_REGISTRATION_STRING as the developer with auth_key, the problem is described below import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; public class C2DMSendMessage { private final static String AUTH =

HttpURLConnection sending JSON POST request to Apache/PHP

风格不统一 提交于 2019-11-30 09:34:58
I'm struggling with HttpURLConnection and OutputStreamWriter. The code actually reaches the server, as I do get a valid error response back. A POST request is made, but no data is received server-side. Any hints to proper usage of this thingy is highly appreciated. The code is in an AsyncTask protected JSONObject doInBackground(Void... params) { try { url = new URL(destination); client = (HttpURLConnection) url.openConnection(); client.setDoOutput(true); client.setDoInput(true); client.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); client.setRequestMethod("POST"); /

Android HttpURLConnection: Handle HTTP redirects

断了今生、忘了曾经 提交于 2019-11-30 08:58:19
I'm using HttpURLConnection to retrieve an URL just like that: URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(true); // ... I now want to find out if there was a redirect and if it was a permanent (301) or temporary (302) one in order to update the URL in the database in the first case but not in the second one. Is this possible while still using the redirect handling of HttpURLConnection and if, how? Simply call getUrl() on URLConnection instance after calling getInputStream() : URLConnection con = new

Android: HttpsUrlConnection with Authenticator for Basic Authentication iterates forever when password is wrong (on 401 response)

二次信任 提交于 2019-11-30 08:51:39
I am using an HttpsUrlConnection with Basic Authentication by using an Authenticator and setting a default Authenticator object like this: Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("user", "userpass" .toCharArray()); } }); When I access my web-service the connection calls my getPasswordAuthentication() method to get the credentials and sends this to the web-server. This works allright as long as the password is correct. :) However, it just happened that someone changed the basic authentication

Why timeout value is not respected by android HttpURLConnection?

主宰稳场 提交于 2019-11-30 08:42:29
I have the following code block: try { URL url = new URL("http://site-to-test.com/nonexistingpage.html"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("User-Agent", CoreProtocolPNames.USER_AGENT); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(500); // timeout is in milliseconds urlc.connect(); if (urlc.getResponseCode() == 404) { // Server was reachable Log.i(TAG, "Server is reachable"); } } catch (MalformedURLException mue) { Log.e(TAG, "MalformedURLException: " + mue.getMessage()); } catch (IOException e) { Log.e(TAG,

HTTP POST request with JSON String in JAVA

让人想犯罪 __ 提交于 2019-11-30 05:12:58
I have to make a http Post request using a JSON string I already have generated. I tried different two different methods : 1.HttpURLConnection 2.HttpClient but I get the same "unwanted" result from both of them. My code so far with HttpURLConnection is: public static void SaveWorkflow() throws IOException { URL url = null; url = new URL(myURLgoeshere); HttpURLConnection urlConn = null; urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoInput (true); urlConn.setDoOutput (true); urlConn.setRequestMethod("POST"); urlConn.setRequestProperty("Content-Type", "application/json");

HttpURLConnection downloaded file name

ⅰ亾dé卋堺 提交于 2019-11-30 05:11:42
Is it possible to get the name of a file downloaded with HttpURLConnection? URL url = new URL("http://somesite/getFile?id=12345"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setAllowUserInteraction(false); conn.setDoInput(true); conn.setDoOutput(true); conn.connect(); InputStream is = conn.getInputStream(); In the example above I cannot extract the file name from the URL, but the server will send me the file name in some way. Pau Kiat Wee You could use HttpURLConnection.getHeaderField(String name) to get the Content-Disposition header,

Java multiple connection downloading

我的未来我决定 提交于 2019-11-30 05:03:36
I wanted to get some advice, I have started on a new project to create a java download accelerator that will use multiple connections. I wanted to know how best to go about this. So far I have figured out that i can use HttpUrlConnection and use the range property, but wanted to know an efficient way of doing this. Once i have download the parts from the multiple connections i will then have to join the parts so that we end up with a fully downloaded file. Thanks in advance :) Syed M Shaaf Get the content length of the file to download. Divide it according to a criteria (size, speed, …). Run

Android: How to programatically login to website and retrieve data from it?

不打扰是莪最后的温柔 提交于 2019-11-30 04:08:16
Ok, guys, so I'm recently developing android app that saves user's ID and PASSWORD to SharedPreferences. Now, when the user starts app second time, he will be redirected directly to MainActivity with a few options in listView. And now I have a huge headache and it's driving me REAL crazy. I cannot login to website and fetch the data to my phone. I've tried using Http(s)UrlConnection, HttpClient, but it just doesn't seem to work for me. All I get from POST method is a source code of login page. Now, there is login page: https://medeine.vgtu.lt/studentams/login.jsp?klb=en and my target page:

HttpURLConnection的使用  

独自空忆成欢 提交于 2019-11-30 03:14:17
HttpURLConnection的使用 /* * URL请求的类别分为二类,GET与POST请求。二者的区别在于: * a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, * b:) post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。 */ // url 对象表示的是一个指定的资源 URL url = new URL(" http://localhost:8080/TestHttpURLConnectionPro.do "); // url 对象的 op enConnection() 方法返回一个 HttpURLConnection 对象 , 这个对象表示应用程序和 url 之间的通信连接 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在 // http正文内,因此需要设为true, 默认情况下是false; urlConn.setDoOutput(true); // 设置是否从httpUrlConnection读入,默认情况下是true; urlConn.setDoInput(true); // Post