httpurlconnection

Parse JSON from HttpURLConnection object

白昼怎懂夜的黑 提交于 2019-11-26 07:19:55
问题 I am doing basic http auth with the HttpURLConnection object in Java. URL urlUse = new URL(url); HttpURLConnection conn = null; conn = (HttpURLConnection) urlUse.openConnection(); conn.setRequestMethod(\"GET\"); conn.setRequestProperty(\"Content-length\", \"0\"); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); conn.connect(); if(conn.getResponseCode()==201 || conn.getResponseCode()==200) { success = true; } I am

Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?

微笑、不失礼 提交于 2019-11-26 06:37:58
问题 I\'m trying to write out to URLConnection#getOutputStream, however, no data is actually sent until I call URLConnection#getInputStream. Even if I set URLConnnection#doInput to false, it still will not send. Does anyone know why this is? There\'s nothing in the API documentation that describes this. Java API Documentation on URLConnection: http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html Java\'s Tutorial on Reading from and Writing to a URLConnection: http://download

Read error response body in Java

佐手、 提交于 2019-11-26 06:32:40
问题 In Java, this code throws an exception when the HTTP result is 404 range: URL url = new URL(\"http://stackoverflow.com/asdf404notfound\"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.getInputStream(); // throws! In my case, I happen to know that the content is 404, but I\'d still like to read the body of the response anyway. (In my actual case the response code is 403, but the body of the response explains the reason for rejection, and I\'d like to display that to

Can you explain the HttpURLConnection connection process?

夙愿已清 提交于 2019-11-26 06:09:38
问题 I am using HTTPURLConnection to connect to a web service. I know how to use HTTPURLConnection but I want to understand how it works. Basically, I want to know the following: On which point does HTTPURLConnection try to establish a connection to the given URL? On which point can I know that I was able to successfully establish a connection? Are establishing a connection and sending the actual request done in one step/method call? What method is it? Can you explain the function of

Persistent HttpURLConnection in Java

大城市里の小女人 提交于 2019-11-26 04:54:03
问题 I am trying to write a java program that will automatically download and name some of my favorite web comics. Since I will be requesting multiple objects from the same domain, I wanted to have a persistent http connection that I could keep open until all the comics have been downloaded. Below is my work-in-progress. How do I make another request from the same domain but different path without opening a new http connection? import java.io.BufferedReader; import java.io.IOException; import java

Sending a JSON HTTP POST request from Android

一个人想着一个人 提交于 2019-11-26 02:54:49
问题 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();

How to handle cookies in httpUrlConnection using cookieManager

梦想的初衷 提交于 2019-11-26 02:30: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

How to send PUT, DELETE HTTP request in HttpURLConnection?

时光怂恿深爱的人放手 提交于 2019-11-26 02:27:34
问题 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. 回答1: To perform an HTTP PUT: URL url = new URL("http://www.example.com/resource"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true);

POST request send json data java HttpUrlConnection

谁说我不能喝 提交于 2019-11-26 02:06:03
问题 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

Upload large file in Android without outofmemory error

不问归期 提交于 2019-11-26 01:58:24
问题 My upload code as below: String end = \"\\r\\n\"; String twoHyphens = \"--\"; String boundary = \"*****\"; try { URL url = new URL(ActionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod(\"POST\"); con.setRequestProperty(\"Connection\", \"Keep-Alive\"); con.setRequestProperty(\"Accept\", \"text/*\"); con.setRequestProperty(\"Content-Type\", \"multipart/form-data; boundary=\" +