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());
https://github.com/Dreampie/Resty resty-client 以流的方式写入数据
###### @Dreampie 谢谢######回复 @Jp_ : DataOutputStream######回复 @Jp_ : 以流的方式 发送参数 也就是post写入参数######大文件以流的形式写入,我就想知道像请求参数那类的,又没有别的方式设置传递版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。