HttpURLConnection类的调用setRequestProperty功能 [英] setRequestProperty function of HttpURLConnection

查看:6846
本文介绍了HttpURLConnection类的调用setRequestProperty功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了Android的POST请求,并上传图片到服务的成功。

I achieve the POST request in Android and upload a picture to service successful.

我没有使用调用setRequestProperty功能;但我想知道这个功能的影响是什么。

I did not use the setRequestProperty function; But I want to know what the effect about this function is.

这是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();

谁能解释HttpURLConnection的?

Could anyone explain the effect of setRequestProperty function in HttpURLConnection?

推荐答案

主要调用setRequestProperty用来设置以下事情按要求

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);

Connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");

有时它成为必要的,你必须指定内容类型进行连接。

Sometimes it become necessary that you have to specify Content-type for the connection.

这篇关于HttpURLConnection类的调用setRequestProperty功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆