Android Soap 请求在使用 HttpUrlConnection 时出错



我使用 HttpUrlconnection 类将 Soap 请求作为 http 请求发送,但收到 405 错误。 我想知道我在 http POST 请求中发送的内容是否正确 而且我不想使用任何库或 api,例如 ksoap 等......

@Override
protected String doInBackground(String... params) {
String login_url="http://172.16.149.1/cms";
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
httpURLConnection.setRequestProperty("SOAPAction", "http://tempuri.org/Login");
String xml="<?xml version="1.0" encoding="utf-8"?>" +
"+<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">"+
"<soap:Body>"+
"<Login xmlns="http://tempuri.org">"+
"<id>string</id>"+
"<password>string</password>"+
"</Login>"+
"</soap:Body>";
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
bufferedWriter.write(xml);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
int status=httpURLConnection.getResponseCode();
if(status == 200)
{

InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = ""+status;
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
return result;}
else
return status+"";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

和 SOAP 请求如下来自 Web 服务

POST /cms/Service.asmx HTTP/1.1
Host: 172.16.149.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Login"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Login xmlns="http://tempuri.org/">
<id>string</id>
<password>string</password>
</Login>
</soap:Body>
</soap:Envelope>

您是否尝试过更改httpURLConnection.setRequestMethod("GET");

httpURLConnection.setRequestMethod("POST");

相关内容

  • 没有找到相关文章

最新更新