HttpURLConnection 中的内容长度
content-length in HttpURLConnection
我正在尝试将 x-www-form-urlencoded
中的正文发送到我的本地网站 API。我可以毫无问题地制作 GET
。这是我的工作:
String urlParameters = "user=User&label=GPU+Temp&file=src%2FDataSource0.txt&backgroundColor=rgb(255%2C255%2C255%2C0)&borderColor=rgb(255%2C255%2C255%2C0)&pointBorderColor=rgb(255%2C255%2C255%2C0)&pointHoverBackgroundColor=rgb(255%2C255%2C255%2C0)&pointHoverBorderColor=rgb(255%2C255%2C255%2C0)&min=0&max=100&stepSize=50";
byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));
int postDataLength = postData.length;
String request = "http://192.168.1.30:6262/api/values";
URL url = new URL(request);
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setInstanceFollowRedirects(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
con.connect();
java.net.ProtocolException: content-length promised 598 bytes, but received 0
所以这意味着我没有在我的 POST
中发送任何数据,这是怎么回事?
使用 con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
您只是发送 postDataLength
的长度,您实际上并没有设置它的正文。为了将您的变量实际发送到网络服务,您需要在调用 connect()
:
之前执行此操作
OutputStream os = con.getOutputStream();
os.write(urlParameters.getBytes("UTF-8"));
os.close();
你需要这样做:
body = datos.toString();
content_bytes = body.getBytes("UTF-8");
content_length = content_bytes.length;
// establecer headers.
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setRequestProperty( "Content-Length", Integer.toString( content_length ) );
.....
dataOutputStream = new DataOutputStream( connection.getOutputStream() );
dataOutputStream.write(body.getBytes("UTF-8"));
我正在尝试将 x-www-form-urlencoded
中的正文发送到我的本地网站 API。我可以毫无问题地制作 GET
。这是我的工作:
String urlParameters = "user=User&label=GPU+Temp&file=src%2FDataSource0.txt&backgroundColor=rgb(255%2C255%2C255%2C0)&borderColor=rgb(255%2C255%2C255%2C0)&pointBorderColor=rgb(255%2C255%2C255%2C0)&pointHoverBackgroundColor=rgb(255%2C255%2C255%2C0)&pointHoverBorderColor=rgb(255%2C255%2C255%2C0)&min=0&max=100&stepSize=50";
byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));
int postDataLength = postData.length;
String request = "http://192.168.1.30:6262/api/values";
URL url = new URL(request);
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setInstanceFollowRedirects(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
con.connect();
java.net.ProtocolException: content-length promised 598 bytes, but received 0
所以这意味着我没有在我的 POST
中发送任何数据,这是怎么回事?
使用 con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
您只是发送 postDataLength
的长度,您实际上并没有设置它的正文。为了将您的变量实际发送到网络服务,您需要在调用 connect()
:
OutputStream os = con.getOutputStream();
os.write(urlParameters.getBytes("UTF-8"));
os.close();
你需要这样做:
body = datos.toString();
content_bytes = body.getBytes("UTF-8");
content_length = content_bytes.length;
// establecer headers.
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setRequestProperty( "Content-Length", Integer.toString( content_length ) );
.....
dataOutputStream = new DataOutputStream( connection.getOutputStream() );
dataOutputStream.write(body.getBytes("UTF-8"));