开发者社区> 问答> 正文

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

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

展开
收起
kun坤 2020-06-07 14:34:50 690 0
1 条回答
写回答
取消 提交回答
  • https://github.com/Dreampie/Resty   resty-client 以流的方式写入数据

    ###### @Dreampie 0谢谢######回复 @Jp_ : DataOutputStream######回复 @Jp_ : 以流的方式 发送参数 也就是post写入参数######大文件以流的形式写入,我就想知道像请求参数那类的,又没有别的方式设置传递
    2020-06-07 14:34:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载