httpurlconnection

How do I do a HTTP GET in Java? [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-26 01:46:55
问题 This question already has an answer here: How to use java.net.URLConnection to fire and handle HTTP requests 11 answers How do I do a HTTP GET in Java? 回答1: If you want to stream any webpage, you can use the method below. import java.io.*; import java.net.*; public class c { public static String getHTML(String urlToRead) throws Exception { StringBuilder result = new StringBuilder(); URL url = new URL(urlToRead); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn

Android download binary file problems

不羁岁月 提交于 2019-11-26 01:46:37
问题 I am having problems downloading a binary file (video) in my app from the internet. In Quicktime, If I download it directly it works fine but through my app somehow it get\'s messed up (even though they look exactly the same in a text editor). Here is a example: URL u = new URL(\"http://www.path.to/a.mp4?video\"); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod(\"GET\"); c.setDoOutput(true); c.connect(); FileOutputStream f = new FileOutputStream(new File(root,

Sending files using POST with HttpURLConnection

柔情痞子 提交于 2019-11-26 01:28:24
问题 Since the Android developers recommend to use the HttpURLConnection class, I was wondering if anyone can provide me with a good example on how to send a bitmap \"file\" (actually an in-memory stream) via POST to an Apache HTTP server. I\'m not interested in cookies or authentication or anything complicated, but I just want to have a reliable and logic implementation. All the examples that I\'ve seen around here look more like \"let\'s try this and maybe it works\". Right now, I have this code

Connecting to remote URL which requires authentication using Java

隐身守侯 提交于 2019-11-26 00:19:29
问题 How do I connect to a remote URL in Java which requires authentication. I\'m trying to find a way to modify the following code to be able to programatically provide a username/password so it doesn\'t throw a 401. URL url = new URL(String.format(\"http://%s/manager/list\", _host + \":8080\")); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 回答1: You can set the default authenticator for http requests like this: Authenticator.setDefault (new Authenticator() { protected

org.apache.http.entity.FileEntity is deprecated in Android 6 (Marshmallow)

我怕爱的太早我们不能终老 提交于 2019-11-26 00:17:13
问题 I\'m upgrading an app to API 23 where org.apache.http is deprecated. My current (deprecated) code looks like this: HttpClient httpClient = new DefaultHttpClient(); File file = new File(attr.Value); String url = server_url; HttpPost request = new HttpPost(url); FileEntity fileEntity = new FileEntity(file, \"image/png\"); request.setEntity(fileEntity); HttpResponse response = httpClient.execute(request); String output = getContent(response.getEntity().getContent()); I\'ve found some suggestions

Java - sending HTTP parameters via POST method easily

…衆ロ難τιáo~ 提交于 2019-11-25 23:57:06
问题 I am successfully using this code to send HTTP requests with some parameters via GET method void sendRequest(String request) { // i.e.: request = \"http://example.com/index.php?param1=a&param2=b&param3=c\"; URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod(\"GET\"); connection.setRequestProperty(\"Content-Type\", \"text/plain\"); connection

HTTPURLConnection Doesn't Follow Redirect from HTTP to HTTPS

≯℡__Kan透↙ 提交于 2019-11-25 23:37:35
问题 I can\'t understand why Java\'s HttpURLConnection does not follow an HTTP redirect from an HTTP to an HTTPS URL. I use the following code to get the page at https://httpstat.us/: import java.net.URL; import java.net.HttpURLConnection; import java.io.InputStream; public class Tester { public static void main(String argv[]) throws Exception{ InputStream is = null; try { String httpUrl = \"http://httpstat.us/301\"; URL resourceUrl = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection

How to add parameters to HttpURLConnection using POST using NameValuePair

匆匆过客 提交于 2019-11-25 21:48:33
问题 I am trying to do POST with HttpURLConnection (I need to use it this way, can\'t use HttpPost ) and I\'d like to add parameters to that connection such as post.setEntity(new UrlEncodedFormEntity(nvp)); where nvp = new ArrayList<NameValuePair>(); having some data stored in. I can\'t find a way how to add this ArrayList to my HttpURLConnection which is here: HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); http = https; http

How to use java.net.URLConnection to fire and handle HTTP requests

你离开我真会死。 提交于 2019-11-25 21:33:34
问题 Use of java.net.URLConnection is asked about pretty often here, and the Oracle tutorial is too concise about it. That tutorial basically only shows how to fire a GET request and read the response. It doesn\'t explain anywhere how to use it to among others perform a POST request, set request headers, read response headers, deal with cookies, submit a HTML form, upload a file, etc. So, how can I use java.net.URLConnection to fire and handle \"advanced\" HTTP requests? 回答1: First a disclaimer

How do I do a HTTP GET in Java? [duplicate]

爱⌒轻易说出口 提交于 2019-11-25 21:01:56
This question already has an answer here: How to use java.net.URLConnection to fire and handle HTTP requests 11 answers How do I do a HTTP GET in Java? Kalpak If you want to stream any webpage, you can use the method below. import java.io.*; import java.net.*; public class c { public static String getHTML(String urlToRead) throws Exception { StringBuilder result = new StringBuilder(); URL url = new URL(urlToRead); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));