HttpURLConnection的setRequestProperty函数的作用

002大破天幕杀机 2013-03-11 04:32:50
在android上实现了POST请求,成功上传给服务器一张图片。虽然我代码中没有用到setRequestProperty这个函数,但是不知道它的作用是什么?
代码如下:
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();
...全文
98480 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinyu116301 2015-10-22
  • 打赏
  • 举报
回复
学习一下。。。。。
uestczhangchao 2014-09-18
  • 打赏
  • 举报
回复
HTTP sends such requests with a Content-Type: multipart/form-data header or a Content-Type: multipart/mixed header and a multipart body, like this: Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz] where the boundary specifies the delimiter string between the different parts of the body.
楠楠2015 2014-04-19
  • 打赏
  • 举报
回复
可不可以把代码贴全啊,本人Android菜鸟,希望能多学习
Simple-520 2013-07-19
  • 打赏
  • 举报
回复
请问楼主,如何设置HTTP头 版本为HTTP2.0 ??? 急!qq:348224249
常常要奋斗 2013-04-13
  • 打赏
  • 举报
回复
恩,3楼正解吧~
问答小助手 2013-03-13
  • 打赏
  • 举报
回复
setRequestProperty 主要是用来设置下面的功能
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
或者
Connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
有时候你需要给 connection 指定 Content-type。
sligner 2013-03-11
  • 打赏
  • 举报
回复
设置头信息的,比如格式,UA等,不设置自然有默认的,一般的请求倒不需要去设置,可以去看看android里的DefaultHttpClient里也有设置头信息的
zyxel60b 2013-03-11
  • 打赏
  • 举报
回复
setRequestProperty主要是设置HttpURLConnection请求头里面的属性 比如Cookie、User-Agent(浏览器类型)等等,具体可以看HTTP头相关的材料 至于要设置什么这个要看服务器端的约定
  • 打赏
  • 举报
回复
服务器的代码如下:

	ServletInputStream inputStream = ((ServletRequest) request)
				.getInputStream();
		int len, offset, count;
		count = offset = 0;
		byte[] buffer = new byte[1024];

		try {
			FileOutputStream outputStream = new FileOutputStream("D:\\123.jpg");
			while ((len = inputStream.readLine(buffer, 0, 1024)) != -1) {
				offset += len;
				outputStream.write(buffer, 0, len);
			}
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			System.out.println(e.toString());
		}

		PrintWriter out = response.getWriter();
		out.println("ok");

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧