0

我想向服务器发送信息,但它只是看到的部分信息Content-Length是不正确的。

我的代码:

private static String boundary = `BOUNDARY`;
private static String break = `\r\n`;

String url = `https://`;
URL uUrl= new URL(url);

HttpsURLConnection con = (HttpsURLConnection) uUrl.openConnection();

con.setDoOutput(true);
con.setDoInput (true);
con.setUseCaches(false); 

con.setRequestMethod(`POST`);
con.setRequestProperty(`Content-Type`, `multipart/mixed; boundary=` + boundary);


//boundary
boundary = `--BOUNDARY` + break +   `Content-Type: application/x-www-form-urlencoded` + break;
boundary = boundary + `Content-Length: ` + `XXXXX`.length();
boundary = boundary + break+ break;
boundary = boundary + `XXXXX` + break;

boundary = boundary + break;
boundary = boundary + `--BOUNDARY` + break+   `Content-Type: application/x-ups-binary` + break;
boundary = boundary + `Content-Length: ` + sContent.length() + break;
boundary = boundary + break;
boundary = boundary + sContent + break;

boundary = boundary + break;
boundary = boundary + `--BOUNDARY--`;

DataOutputStream body = new DataOutputStream(con.getOutputStream());

body .writeBytes(boundary);
body .flush();      
body .close();

只发送了 680 个字节,但应该是 2000 左右。

命令:

System.out.println(con.getContentLength());

结果:

680

此命令未更改Content-Length

con.setRequestProperty(`Content-Length`, `2000`);
4

0 回答 0