大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
I achieve the POST request in Android and upload a picture to service successful.
I did not use the setRequestProperty function; But I want to know what the effect about this function is.
This is the code:
URL url = new URL(“http://192.168.191.104:8080/myapp/servlet/MyServlet”);
HttpURLConnection connection = ((HttpURLConnection) url
.openConnection());
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod(“POST”);
connection.connect();
OutputStream out = connection.getOutputStream();
int len;
byte[] buffer = new byte[1024];
// 读取文件
FileInputStream fileInputStream = new FileInputStream(Environment
.getExternalStorageDirectory().getAbsolutePath() + “/123.jpg”);
while ((len = fileInputStream.read(buffer, 0, 1024)) != -1) {
out.write(buffer);
}
out.flush();
out.close();
fileInputStream.close();
InputStream input = connection.getInputStream();
while ((len = input.read(buffer)) != -1) {
Log.i(“tag”, “data:” + new String(buffer, 0, len));
}
input.close();
connection.disconnect();
Could anyone explain the effect of setRequestProperty function in HttpURLConnection?
解决方案
Mainly setRequestProperty is used to set below things as per the requirement
connection.setRequestProperty(“Connection”, “Keep-Alive”);
connection.setRequestProperty(“Content-Type”, “multipart/form-data;boundary=”+boundary);
or
Connection.setRequestProperty(“Content-Type”, “text/plain; charset=utf-8”);
Sometimes it become necessary that you have to specify Content-type for the connection.
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195095.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...