httpurlconnection

Android HttpsUrlConnection eofexception

余生长醉 提交于 2019-11-27 06:57:42
I have a problem where my HttpsURLConnection will throw an EOFException when i try to read any input. The code works for some network calls, but fails on others. If i try and read anything from the connection, it fails with the aforementioned error. Example: urlConnect.getResponseCode() // will throw error urlConnect.getResponseMessage() // will throw error BufferedInputStream in = new BufferedInputStream(urlConnect.getInputStream()); //will throw error Here is the stack trace for each: getResponse: 03-14 09:49:18.547: W/System.err(6270): java.io.EOFException 03-14 09:49:18.547: W/System.err

HttpUrlConnection.openConnection fails second time

元气小坏坏 提交于 2019-11-27 06:27:46
I know this issue should be fixed with System.setProperty("http.keepAlive", "false"); before openConnection, but that didn't work to me. First try on this code works, second one fails. Even if i try this request after less than 5 seconds, it also works. If i wait more than that, it fails again This is my code: System.setProperty("http.keepAlive", "false"); HttpURLConnection conn = (HttpURLConnection) mURL.openConnection(); conn.setUseCaches(false); conn.setRequestProperty("Connection","Keep-Alive"); conn.setRequestProperty("User-Agent", useragent); conn.setConnectTimeout (30000) ; conn

android download pdf from url then open it with a pdf reader

女生的网名这么多〃 提交于 2019-11-27 06:25:20
I am trying to write an app to download pdfs from url, store them on sd, then open by adobe pdf reader or other apps which ever able to open the pdf. untill now, i had "successfully downloaded and stored it on Sd card" (but everytime when i try to open the pdf with a pdf reader, reader crash and say unexpected error occurs), for example, http://maven.apache.org/maven-1.x/maven.pdf here is the code for my downloader: //........code set ui stuff //........code set ui stuff new DownloadFile().execute(fileUrl, fileName); private class DownloadFile extends AsyncTask<String, Void, Void>{ @Override

HttpURLConnection java.io.FileNotFoundException

早过忘川 提交于 2019-11-27 05:18:31
问题 The code below works great if I connect to what seems to be Apache servers, however when I try to connect to my .Net server it throws an error. I am guessing it is a header requirement, but I can not seem to get a successful response no matter what I try. public String Download(String Url) { String filepath=null; try { //set the download URL, a url that points to a file on the internet //this is the file to be downloaded URL url = new URL(Url); //create the new connection HttpURLConnection

How to Reuse HttpUrlConnection? [duplicate]

落爺英雄遲暮 提交于 2019-11-27 05:14:20
This question already has an answer here: Persistent HttpURLConnection in Java 2 answers I am interested in reusing an HttpUrlConnection (as part of a statefull protocol between server and client that I'm developing). I know that there is an Connection=keep-alive header for persistent http. Now, I want to know how to reuse such a conenction. I have written this code: URL u = new java.net.URL("http://localhost:8080/Abc/Def"); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setRequestProperty("Connection", "keep-alive"); c.setHeader("A","B"); c

Java simple code: java.net.SocketException: Unexpected end of file from server

杀马特。学长 韩版系。学妹 提交于 2019-11-27 04:57:48
I wrote some simple code in Java, the method should connect to the website and return the BufferedReader. private BufferedReader getConnection(String url_a) { URL url; try { System.out.println("getting connection"); url = new URL(url_a); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924" + "Epiphany/1.4.4 (Ubuntu)"); inStream = new InputStreamReader(urlConnection.getInputStream()); return new BufferedReader(inStream); } catch (Exception ex) { Logger

Java URLConnection - When do I need to use the connect() method?

别来无恙 提交于 2019-11-27 03:02:56
问题 I have a problem to understand the meaning of the connect() method in the URLConnection class. In the following code, if I use the connect() method, I get the same result if I don't use it. Why (or when) do I need to use it? URL u = new URL("http://example.com"); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect();//with or without it I have the same result InputStream in = conn.getInputStream(); int b; while ((b = in.read()) != -1) { System.out.write(b); } 回答1:

HttpURLConnection implementation

大憨熊 提交于 2019-11-27 02:10:35
问题 I have read that HttpURLConnection supports persistent connections, so that a connection can be reused for multiple requests. I tried it and the only way to send a second POST was by calling openConnection for a second time. Otherwise I got a IllegalStateException("Already connected"); I used the following: try{ URL url = new URL("http://someconection.com"); } catch(Exception e){} HttpURLConnection con = (HttpURLConnection) url.openConnection(); //set output, input etc //send POST //Receive

Cannot write output after reading input

…衆ロ難τιáo~ 提交于 2019-11-27 02:04:35
I'm writing a program that connects to a servlet thanks to a HttpURLConnection but I stuck while checking the url public void connect (String method) throws Exception { server = (HttpURLConnection) url.openConnection (); server.setDoInput (true); server.setDoOutput (true); server.setUseCaches (false); server.setRequestMethod (method); server.setRequestProperty ("Content-Type", "application / xml"); server.connect (); /*if (server.getResponseCode () == 200) { System.out.println ("Connection OK at the url:" + url); System.out.println ("------------------------------------------- ------- "); }

How to send Request payload to REST API in java?

丶灬走出姿态 提交于 2019-11-27 01:01:01
I want to retrieve the JSON data from the following: https://git.eclipse.org/r/#/c/11376/ Request URL: https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService Request Method: POST Request Headers: Accept:application/json Content-Type:application/json; charset=UTF-8 Request Payload: {"jsonrpc":"2.0","method":"changeDetail","params":[{"id":11376}],"id":1} I already tried this answer but I am getting 400 BAD REQUEST . Can anyone help me sort this out? Thanks. Gangaraju The following code works for me. //escape the double quotes in json string String payload="{\"jsonrpc\":\"2.0\",\"method\":\