HttpURLConnection 如何用非拼接url的形式携带参数

Jping 发布于 2015/04/24 10:47
阅读 2K+
收藏 0

HttpURLConnection 如何用非拼接url的形式携带参数?代码如下,客户端不想讲参数直接拼接在url后面,这样不安全。

请大牛帮忙解答,谢谢。

URL url = null;
HttpURLConnection connection = null;
RandomAccessFile randomFile = null;
OutputStream outPutStream = null;
BufferedReader bufferReader = null;
// String result = null;


StringBuffer uploadUrl = new StringBuffer(url1);
Log.i(tag, "upload url:  " + uploadUrl.toString());
try {
url = new URL(uploadUrl.toString());
connection = (HttpURLConnection) url.openConnection();


// connection.setRequestProperty("busiSource", "01");
// connection.setRequestProperty("userId", "4700");


connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type",
"application/octet-stream");
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setAllowUserInteraction(true);
connection.setChunkedStreamingMode(0);
connection.connect();


outPutStream = connection.getOutputStream();
randomFile = new RandomAccessFile(filePath, "rw");
int real = 0;


if (breakPoint > 0 && (breakPoint < fileSize)) {
Log.i(tag, "writeX");
randomFile.seek(breakPoint);
while ((real = randomFile.read(bufferRead)) != -1 && !isStop) {
outPutStream.write(bufferRead, 0, real);
getAlreadyUploadLength = getAlreadyUploadLength + real;
if (getAlreadyUploadLength >= anUpdateSize * updateTimes) {
progress = getAlreadyUploadLength;
updateTimes++;
}
}
} else {
Log.i(tag, "write0");
while ((real = randomFile.read(bufferRead)) != -1 && !isStop) {
outPutStream.write(bufferRead, 0, real);
getAlreadyUploadLength = real + getAlreadyUploadLength;
if (getAlreadyUploadLength >= anUpdateSize * updateTimes) {
progress = getAlreadyUploadLength;
updateTimes++;
}
}
}
outPutStream.flush();
// 返回当前文件读取情况
StringBuffer fileUploadResponse = new StringBuffer();
bufferReader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "utf-8"));
// 发送http请求(头信息),返回输入流,用于读取此次请求的响应信息
String line = "";
while ((line = bufferReader.readLine()) != null) {
fileUploadResponse.append(line);
}
Log.i(tag, "服务器返回的响应信息:" + fileUploadResponse.toString());

加载中
0
Dreampie
Dreampie

https://github.com/Dreampie/Resty   resty-client 以流的方式写入数据

Jping
Jping
@Dreampie 0谢谢
Dreampie
Dreampie
回复 @Jp_ : DataOutputStream
Dreampie
Dreampie
回复 @Jp_ : 以流的方式 发送参数 也就是post写入参数
Jping
Jping
大文件以流的形式写入,我就想知道像请求参数那类的,又没有别的方式设置传递
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部