httpurlconnection

Java: Display request of an HttpURLConnection before sending

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 01:12:55
I want to make some API calls to a server using HttpURLConnection . But the requests are not successful, returning: <error> <http_status>400 Bad Request</http_status> <message>Unexpected request Content-Type header ''. Expecting 'application/x-www-form-urlencoded'.</message> </error> So I want to check what the "real" content is that is being sent to the server. By real content I mean the exact HTTP request. Any ideas how I can see this? Edit: Based on the first answers here I should clarify my problem: I want to avoid using an external program like HTTP sniffer or anything and I was hoping

Tomcat, HTTP Keep-Alive and Java's HttpsUrlConnection

点点圈 提交于 2019-11-29 00:59:57
问题 I have two Tomcat servers that need to maintain a persistent connection to cut down on SSL handshaking. One server (the proxy) sits in a DMZ while the other one is safely behind another firewall. The proxy basically just runs a simple servlet that does some sanity checking before forwarding requests over to the secure machine. On an intial request the machines exchange certificates before performing the real work. Therefore I'd like to maintain a persistent connection with a timeout of a few

Android - HttpUrlConnection is not closing. Eventually results to SocketException

最后都变了- 提交于 2019-11-28 23:45:53
I am encountering some problems with the HttpUrlConnection in devices running Jellybean (4.1 - 4.3) wherein connections are not closed and results to a SocketException "Too many open files" after executing a number of times. I do call HttpUrlConnection.disconnect() and am closing all the Inputstream, Outputstream, Reader and Writers in a finally block. Going to adb shell and executing a netstat shows all the connections created by the application are left in CLOSE_WAIT state. InputStream inputStream = httpUrlConnection.getInputStream(); // After calling inputStream.read() then the problem

URLConnection does not get the charset

一曲冷凌霜 提交于 2019-11-28 23:14:35
I'm using URL.openConnection() to download something from a server. The server says Content-Type: text/plain; charset=utf-8 But connection.getContentEncoding() returns null . What up? Waldheinz This is documented behaviour as the getContentEncoding() method is specified to return the contents of the Content-Encoding HTTP header, which is not set in your example. You could use the getContentType() method and parse the resulting String on your own, or possibly go for a more advanced HTTP client library like the one from Apache . Buhake Sindi The value returned from URLConnection

Need an example of HttpResponseCache in Android

雨燕双飞 提交于 2019-11-28 21:32:18
Hi I am trying to use the HttpResponseCache introduced in Android 4.The docs do talk clearly about how to install the cache but I am at a complete loss on how to cache Images downloaded from the net.Earlier I was using the DiskLruCache to cache them. Would anyone point me towards some examples of working code where HttpResponseCache has been used.. Edit:- Can someone tell me what I am doing wrong here:- MainActivity.java public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final long httpCacheSize = 10 * 1024 * 1024; //

How to read full response from HttpURLConnection?

我的未来我决定 提交于 2019-11-28 21:21:34
I make some proxy server in andorid which modify http headers, it works ok, but I have to forward full response to 'top layer'. How I can read whole response (all headers, content, everything) from HttpURLConnection? HttpURLConnection httpURLConnection; URL url = new URL(ADDRESS); httpURLConnection = (HttpURLConnection) url.openConnection(); // add headers, write output stream, flush if (httpURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK) { Map<String, List<String>> map = httpURLConnection.getHeaderFields(); System.out.println("Printing Response Header...\n"); for (Map.Entry

Android HTTPUrlConnection : how to set post data in http body?

﹥>﹥吖頭↗ 提交于 2019-11-28 20:02:32
I've already created my HTTPUrlConnection : String postData = "x=val1&y=val2"; URL url = new URL(strURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Set-Cookie", sessionCookie); conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length)); // How to add postData as http body? conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); I don't know how to set postData in http body. How to do so?

Reusing HttpURLConnection so as to keep session alive

一曲冷凌霜 提交于 2019-11-28 19:41:53
We have an Android application that requires the user to enter an answer to a Captcha. The Captcha is generated on our server. When the replies, it is sent to the server for verifying. Problem is that since I have to close the HttpURLConnection after the request for the Captcha I then find that the reply is running on a different session on the sever. Because of this the Captcha check fails since it is session dependant. Is there a way to keep the connection alive or should I be following a different path? I know that in the equivalent iPhone application they remain "connected" and thus have

How to send Https Post request in java

孤街浪徒 提交于 2019-11-28 16:42:53
问题 I want to login to application from java code. Here is my code... String httpsURL = "https://www.abcd.com/auth/login/"; String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8"); query += "&"; query += "password="+URLEncoder.encode("abcd","UTF-8") ; URL myurl = new URL(httpsURL); HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-length", String.valueOf(query.length())); con.setRequestProperty("Content-Type

Should HttpURLConnection with CookieManager automatically handle session cookies?

巧了我就是萌 提交于 2019-11-28 16:06:42
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 have to manually extract cookies from responses and resend them in requests on my own or is