httpurlconnection

How to use different cookies for each connection using HttpURLConnection and the CookieManager in Java

坚强是说给别人听的谎言 提交于 2019-12-05 21:48:33
问题 I needed to connect to a website from multiple threads simultaneously using HttpURLConnection, but use different cookies for each connection. Since Java only supports setting a global CookieManager, I've implemented the following hack. Instead of calling CookieHandler.setDefault(new CookieManager()) , I've implemented a custom CookieHandler which uses a different CookieStore instance for every thread, which is cleared after each request. I've created class called SessionCookieManager based on

Tomcat memory leak warning on HttpURLConnection

守給你的承諾、 提交于 2019-12-05 21:40:56
I have the following warning in Tomcat 8.5 I'm not sure I can ignore WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [AppName] appears to have started a thread named [pool-20-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.net.SocketInputStream.socketRead0(Native Method) java.net.SocketInputStream.socketRead(SocketInputStream.java:116) java.net.SocketInputStream.read(SocketInputStream.java:171) java.net.SocketInputStream.read(SocketInputStream.java:141) sun

HttpURLConnection 请求

大城市里の小女人 提交于 2019-12-05 19:40:00
我们经常在程序中发送Web请求,但是也经常在请求中出现乱码问题。下面的代码是发送请求的通用方法,但是在某些环境下中文会乱码,如何解决乱码问题呢?一开始的时候,我只想到对传入的参数进行 URLEncoder.encode(params.get("title"),"UTF-8"); 然后在服务端发现接收的数据还是乱码,根本不用解码已经乱了,后面高人指点如下: URLEncoder.encode(URLEncoder.encode(params.get("title"),"UTF-8"),"UTF-8"); 然后在服务器端如下: URLDecoder.decode(jsonvalue.get("title").toString(),"UTF-8"); 终于得到了想要的中文。 总结:网络传输时,数据会被解析两次,第一次是在网络中,第二次是在服务器。如果我们在传输网络数据的时候没有加码,那么会解析为乱码,所以我们避免中文乱码,需要加码两次,第一次是让网络解析,解析过后还是加码的所有不会乱码,到服务器在解码问题就解决了。 /** * 客户端发送HTTP请求通用POST方法 * @param url * @param params * @return * @throws Exception */ public static String postHttpRequest(String url ,

How to switch from HttpURLConnection to HttpClient

自闭症网瘾萝莉.ら 提交于 2019-12-05 16:22:20
this is my first question, so please, bear with me. I have a Swing application, which gets data in a for of XML from a server through HttpURLConnection. Now I'm trying to create a constant request-respond connection with the server, to check if there are any updates for the application (as the checking has to be done regularly and often (every second or so)). In some question's comment I read that it would be better to use Apache HttpClient instead of HttpURLConnection to maintain a live connection, but I can't find any good example how to go from my current code to the one with HttpClient.

HttpURLConnection: java.lang.IllegalStateException: Already connected

时光怂恿深爱的人放手 提交于 2019-12-05 16:09:48
问题 I'm trying to use HttpURLClient to send some POST data to a server using the HttpRestClient class shown below. When executing conn.setDoInput(true); I get java.lang.IllegalStateException: Already connected I uninstalled the app, and still get the same error. In all the example I've seen openConnection is called before setDoInput . If, as its name suggests, openConnection opens a connection, it should never be used before `setDoInput, right? What am I missing? Maybe at some point it crashed

Android - Using HttpURLConnection to POST XML data

馋奶兔 提交于 2019-12-05 13:32:07
I've run into a bit of a deadend and need a bit of help (please)! I'm very new to Android Dev (and to coding in general). Basically I need to POST XML data to a URL using HttpURLConnection but can't get it to work. I've got my app reading and pasrsing XML data from a GET request but finding the POST part difficult. I've looked at creating a NameValuePair array but not sure how to do this with the XML structure I am needing to post. The XML data will look like this: <Sheet> <Job>jobNumber</Job> <Task>taskNumber</Task> <UserID>3</UserID> <Date>systemDateFormatted</Date> <Minutes>timeToLog<

Send UTF-8 chars via HttpURLConnection failing

倾然丶 夕夏残阳落幕 提交于 2019-12-05 13:29:59
I have spent half Sunday on this now I need help: I want to send a String including special chars UTF-8 encoded to a server using Java HttpURLConnection. The correct encoding of the chars fails. Example: strToSend: ä ù € strUrlEncoded: %C3%A4+%C3%B9+%E2%82%AC strReceived:ä ù ⬠My code: urlConnection = (HttpURLConnection) new URL("http://localhost:8080/NetworkingServer/ServerServlet").openConnection(); urlConnection.setUseCaches(false); urlConnection.setDoOutput(true); // Triggers POST. urlConnection.setRequestProperty("accept-charset", "UTF-8"); urlConnection.setRequestProperty("content

HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found

好久不见. 提交于 2019-12-05 13:14:24
I have some typical codes which used HttpURLConnection to get a file with an URL. They worked fine in android 1.x and 2.x. But failed in Android 4.1! I searched on the web but found little similar information. Would anybody please help to investigate this issue? private String mURLStr; private HttpURLConnection mHttpConnection; ... url = new URL(mURLStr); ... mHttpConnection = (HttpURLConnection) url.openConnection(); mHttpConnection.setDoOutput(true); mHttpConnection.setRequestMethod("GET"); ... InputStream is = mHttpConnection.getInputStream(); The getInputStream method throws an exception:

Getting http 407 error as an IOException

江枫思渺然 提交于 2019-12-05 11:56:10
I'm using in my android application the HttpURLConnection through a proxy where an authentication is needed. Here is my code and I will explain you after my problem. HttpURLConnection connection = null; int responseCode = -1; try { connection = (HttpURLConnection) myUrl.openConnection(); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(DEFAULT_TIMEOUT); connection.setReadTimeout(DEFAULT_TIMEOUT); responseCode = connection.getResponseCode(); System.out.println("ResponseCode = " + responseCode); } catch (IOException e) { System.out.println("Exception : " + e.getMessage(

Java - HttpUrlConnection returns cached response every time

喜欢而已 提交于 2019-12-05 11:28:51
问题 I'm trying to gather statistical data from Roblox's currency exchange for analysis. Therefore, I need up-to-date data instead of a cached result. However, it seems that no matter what I do, the result is still cached. It seems that the most intuitive option, setUseCaches() , had no effect, and setting the header manually as Cache-Control: no-cache does not seem to work either. I inspected the Cache header using Fiddler2 and saw that its value was Cache-Control: max-age=0 , but it didn't seem