urlconnection

ANDROID java.net.URLConnection send image and data at once

青春壹個敷衍的年華 提交于 2019-12-01 10:27:40
i use java.net.URLConnection to send image and some parameters to server using POST method. My code below is works fine to send image to server, but i'm bit confused to attach some parameter and send to server in one time. I've followed here and here but i think its different method with my code. Here is my snippet code bellow : import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.app.ProgressDialog; import android.util.Log;

ANDROID java.net.URLConnection send image and data at once

雨燕双飞 提交于 2019-12-01 08:07:56
问题 i use java.net.URLConnection to send image and some parameters to server using POST method. My code below is works fine to send image to server, but i'm bit confused to attach some parameter and send to server in one time. I've followed here and here but i think its different method with my code. Here is my snippet code bellow : import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import

connection.setRequestProperty and excplicitly writing to the urloutputstream are they same?

元气小坏坏 提交于 2019-11-30 19:02:38
URL url = new URL("http://www.example.com/comment"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); Is connection.setRequestProperty(key, value); the same as OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write("key=" + value); writer.close(); ? If not, please correct me. No, it is not. The URLConnection#setRequestProperty() sets a request header . For HTTP requests you can find all possible headers here . The writer just writes the request body . In case of

In java, how to create HttpsURLConnection or HttpURLConnection based on the url?

廉价感情. 提交于 2019-11-30 11:39:15
I'm working on a project where I'm creating a class to run http client requests (my class acts as a client). It takes in a url and a request method (GET, POST, PUT, etc) and I want to be able to parse the URL and open a HttpsURLConnection or HttpURLConnection based on whether it is https or http (assume the given urls will always be correct). If I do the following: URLConnection conn = url.openConnection(); Then that will automatically create a URLConnection that can accept both http and https, but if I do this then I can't find any way to set a request method (GET, POST, etc), since only the

Difference between URLConnection, HttpURLConnection and HttpsURLConnection

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 10:55:29
问题 What is the difference between URLConnection , HttpURLConnection and HttpsURLConnection (with SSL). Under what conditions, which one should I use? 回答1: URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only. All three of them are abstract, and implemented

How to send a cookie in a URLConnection?

筅森魡賤 提交于 2019-11-30 08:28:12
问题 What is the proper way to send a 'full' cookie across a URLConnection? I've been using: URL url = new URL(page); URLConnection urlConn = url.openConnection(); urlConn.setRequestProperty("Cookie", myCookie); urlConn.setUseCaches(true); urlConn.connect(); The myCookie value is testCookie=d1lEZk9rSHd3WnpBd2JkWGRhN1RYdz09OkEwQ21pSFJVZzBpVDhhUENaK3ZPV2c9PQ Is there a way to send the Path,Domain, and Expires with it? Do you need to encode the value in some way? 回答1: Well, if you are only setting a

Download binary file from Github using Java

南笙酒味 提交于 2019-11-30 05:27:29
问题 I'm trying to download this file (http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar) with the following method and it doesn't seem to work. I'm getting an empty/corrupt file. String link = "http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar"; String fileName = "ChampionHelper-4.jar"; URL url = new URL(link); URLConnection c = url.openConnection(); c.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET

connection.setRequestProperty and excplicitly writing to the urloutputstream are they same?

寵の児 提交于 2019-11-30 03:02:59
问题 URL url = new URL("http://www.example.com/comment"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); Is connection.setRequestProperty(key, value); the same as OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write("key=" + value); writer.close(); ? If not, please correct me. 回答1: No, it is not. The URLConnection#setRequestProperty() sets a request header . For

Android getContentLength always return -1 when downloading apk file

二次信任 提交于 2019-11-29 18:00:00
I'm using the following code for downloading file in my Android project: URL url = new URL(fileUrl); URLConnection conection= url.openConnection(); conection.setDoOutput(true); conection.connect(); int lenghtOfFile = conection.getContentLength(); If fileUrl is apk, lenghtOfFile always return -1. But if it is image, video type,... lenghtOfFile return is exactly. Why ? I'm using eclipse, Android SDK revision 23. The content length is not always available because by default Android request a GZIP compressed response. Source: Android documentation . Quoting the link: By default, this

In java, how to create HttpsURLConnection or HttpURLConnection based on the url?

谁说我不能喝 提交于 2019-11-29 17:23:29
问题 I'm working on a project where I'm creating a class to run http client requests (my class acts as a client). It takes in a url and a request method (GET, POST, PUT, etc) and I want to be able to parse the URL and open a HttpsURLConnection or HttpURLConnection based on whether it is https or http (assume the given urls will always be correct). If I do the following: URLConnection conn = url.openConnection(); Then that will automatically create a URLConnection that can accept both http and