下面是我用来在我的 Android 应用程序中发送 SOAP 请求的代码,它适用于除一个之外的所有请求。此代码抛出IOException : Content-length exceeded on wr.flush();
when there are Chinese characters in requestBody
variable。
这种情况下的内容长度是409
URL url = new URL(Constants.HOST_NAME);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Modify connection settings
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("SOAPAction", soapAction);
String requestBody = new String(soapRequest.getBytes(),"UTF-8");
int lngth = requestBody.length();
connection.setRequestProperty("Content-Length", (""+lngth));
// Enable reading and writing through this connection
connection.setDoInput(true);
connection.setDoOutput(true);
// Connect to server
connection.connect();
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
wr.write(requestBody);
wr.flush();
wr.close();
任何线索当字符串中有中文字符时出了什么问题?
编辑:我已经删除了 'content-lenght' 标题字段并且它可以工作,但是为什么呢?