httpurlconnection

FileNotFoundException while getting the InputStream object from HttpURLConnection

一世执手 提交于 2019-11-27 00:45:36
I am trying to send a post request to a url using HttpURLConnection (for using cUrl in java). The content of the request is xml and at the end point, the application processes the xml and stores a record to the database and then sends back a response in form of xml string. The app is hosted on apache-tomcat locally. When I execute this code from the terminal, a row gets added to the db as expected. But an exception is thrown as follows while getting the InputStream from the connection java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate at sun.net.www.protocol.http

getRequestProperty(“Authorization”) always returns null

对着背影说爱祢 提交于 2019-11-26 23:09:02
问题 I am trying to read the authorization header for an HTTP request (because I need to add something to it), but I always get null for the header value. Other headers work fine. public void testAuth() throws MalformedURLException, IOException{ URLConnection request = new URL("http://google.com").openConnection(); request.setRequestProperty("Authorization", "MyHeader"); request.setRequestProperty("Stackoverflow", "anotherHeader"); // works fine assertEquals("anotherHeader", request

How to enable wire logging for a java HttpURLConnection traffic?

久未见 提交于 2019-11-26 22:20:51
I've used Jakarta commons HttpClient in another project and I would like the same wire logging output but using the "standard" HttpUrlConnection. I've used Fiddler as a proxy but I would like to log the traffic directly from java. Capturing what goes by the connection input and output streams is not enough because the HTTP headers are written and consumed by the HttpUrlConnection class, so I will not be able to log the headers. Vadzim According to Sun's HttpURLConnection source there is some logging support via JUL . Setup (adjust path as required): -Djava.util.logging.config.file=/full/path

HttpEntity is deprecated on Android now, what's the alternative?

*爱你&永不变心* 提交于 2019-11-26 22:10:23
With the release of Android 5.1, it looks like all the Apache http stuff has been deprecated. Looking at the documentation is useless; they all say This class was deprecated in API level 22. Please use openConnection() instead. Please visit this webpage for further details. Which is fine the first time you read it, but when EVERY class that was deprecated says that, it's not that helpful. Anyway, what are the alternatives for classes like HttpEntity , specifically StringEntity , and MultipartEntity ? I substituted BasicNameValuePair for my own implementation of Android's Pair<T, S> class, and

HttpURLConnection.getResponseCode() returns -1 on second invocation

狂风中的少年 提交于 2019-11-26 21:51:22
I seem to be running into a peculiar problem on Android 1.5 when a library I'm using (signpost 1.1-SNAPSHOT), makes two consecutive connections to a remote server. The second connection always fails with a HttpURLConnection.getResponseCode() of -1 Here's a testcase that exposes the problem: // BROKEN public void testDefaultOAuthConsumerAndroidBug() throws Exception { for (int i = 0; i < 2; ++i) { final HttpURLConnection c = (HttpURLConnection) new URL("https://api.tripit.com/oauth/request_token").openConnection(); final DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(api_key, api

“Illegal State Exception: Already Connected” when using HttpURLConnection

南笙酒味 提交于 2019-11-26 21:47:29
问题 I get an illegal state exception when i set DoOutput to true. public boolean sendLinksToMaster(String ipport, List<String> links){ boolean sent = false; String[] tokens = ipport.split(":"); String data = edu.cis555.searchengine.utils.Utils.generateLinks(links); HttpURLConnection conn=null; try{ String encodedData = URLEncoder.encode(data, "UTF-8"); try{ String ip =tokens[0]; String port = tokens[1]; String path = edu.cis555.searchengine.utils.Constants.URL_ADD_LINK; System.setProperty("http

java.io.IOException: Received authentication challenge is null in ICS 4.0.3

邮差的信 提交于 2019-11-26 21:22:09
问题 I'm trying to logoff from the server. But it returns "0" response code with this exception. I'm using GET verb to do this. LogCat 10-17 14:54:13.261: W/System.err(868): java.io.IOException: Received authentication challenge is null 10-17 14:54:13.284: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397) 10-17 14:54:13.284: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345

How to modify the header of a HttpUrlConnection

坚强是说给别人听的谎言 提交于 2019-11-26 20:49:09
Im trying to improve the Java Html Document a little but i'm running into problems with the HttpUrlConntion . One thing is that some servers block a request if the user agent is a Java VM. Another problem is that the HttpUrlConnection does not set the Referrer or Location header field. Since several sites use these fields to verify that the content was accessed from their own site, I'm blocked here as well. As far as I can see the only resolution is to replace the URL handler of the HTTP protocol. Or is there any way to modify the default HTTP Handler? Open the URL with URL.openConnection .

Java HttpURLConnection doesn't connect when I call connect()

倖福魔咒の 提交于 2019-11-26 20:47:50
问题 I'm trying to write a program to do automated testing on my webapp. To accomplish this, I open up a connection using HttpURLConnection. One of the pages that I'm trying to test performs a 302 redirect. My test code looks like this : URL currentUrl = new URL(urlToSend); HttpURLConnection connection = (HttpURLConnection) currentUrl.openConnection(); connection.connect(); system.out.println(connection.getURL().toString()); So, let's say that urlToSend is http://www.foo.com/bar.jsp, and that this

HttpURLConnection Invalid HTTP method: PATCH

旧街凉风 提交于 2019-11-26 20:28:25
When I try to use a non-standard HTTP Method like PATCH with URLConnection: HttpURLConnection conn = (HttpURLConnection) new URL("http://example.com").openConnection(); conn.setRequestMethod("PATCH"); I get an exception: java.net.ProtocolException: Invalid HTTP method: PATCH at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440) Using a higher level API like Jersey generates the same error. Is there a workaround to issue a PATCH HTTP request? sagar Yes there is workaround for this. Use X-HTTP-Method-Override . This header can be used in a POST request to “fake” other HTTP