【发布时间】:2017-12-01 13:26:48
【问题描述】:
我正在尝试将x-www-form-urlencoded 中的正文发送到我的本地 Web 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 承诺 598 字节,但收到 0
所以这意味着我没有在我的POST 中发送任何数据,怎么会?
【问题讨论】:
-
你需要像 con.getOutputStream().write(postData) 一样发送你的 postData
标签: java android httpurlconnection content-length