httpurlconnection

How to send Request payload to REST API in java?

限于喜欢 提交于 2019-11-26 12:26:56
问题 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. 回答1: The following

FileNotFoundException while getting the InputStream object from HttpURLConnection

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 12:25:33
问题 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

Sending a JSON HTTP POST request from Android

女生的网名这么多〃 提交于 2019-11-26 12:09:50
I'm using the code below to send an http POST request which sends an object to a WCF service. This works ok, but what happens if my WCF service needs also other parameters? How can I send them from my Android client? This is the code I've written so far: StringBuilder sb = new StringBuilder(); String http = "http://android.schoolportal.gr/Service.svc/SaveValues"; HttpURLConnection urlConnection=null; try { URL url = new URL(http); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); urlConnection.setUseCaches(false);

HttpUrlConnection.openConnection fails second time

坚强是说给别人听的谎言 提交于 2019-11-26 12:00:40
问题 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(\

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

陌路散爱 提交于 2019-11-26 11:58:43
问题 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()

How to handle cookies in httpUrlConnection using cookieManager

别等时光非礼了梦想. 提交于 2019-11-26 11:41:38
I have a server request that returns multiple cookies, like that: This is how I'm storing these cookies to the cookieManager: HttpURLConnection connection = ... ; static java.net.CookieManager msCookieManager = new java.net.CookieManager(); msCookieManager.put(COOKIES_URI, connection.getHeaderFields()); This is how I'm adding these cookies to the next connection: connection.setRequestProperty("Cookie", msCookieManager.getCookieStore().get(COOKIES_URI).toString()); Is it the right way to get the cookies from the cookieManager?, I'm quite sure there is a better one... David Ok, the right way to

How to send PUT, DELETE HTTP request in HttpURLConnection?

China☆狼群 提交于 2019-11-26 11:33:14
I want to know if it is possible to send PUT, DELETE request (practically) through java.net.HttpURLConnection to HTTP-based URL. I have read so many articles describing that how to send GET, POST, TRACE, OPTIONS requests but I still haven't found any sample code which successfully performs PUT and DELETE requests. Matthew Murdoch To perform an HTTP PUT: URL url = new URL("http://www.example.com/resource"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestMethod("PUT"); OutputStreamWriter out = new OutputStreamWriter( httpCon

How to Reuse HttpUrlConnection? [duplicate]

故事扮演 提交于 2019-11-26 11:28:39
问题 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

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

三世轮回 提交于 2019-11-26 11:24:45
问题 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

POST request send json data java HttpUrlConnection

南笙酒味 提交于 2019-11-26 10:13:50
I have developed a java code that convert the following cURL to java code using URL and HttpUrlConnection. the curl is : curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}' I have written this code but it always gives HTTP code 400 bad request. I couldn't find what is missing. String url="http://url.com"; URL object=new URL(url); HttpURLConnection con = (HttpURLConnection) object.openConnection(); con.setDoOutput(true); con.setDoInput(true); con