我使用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>
答案 0 :(得分:0)
你有没有尝试改变?
httpURLConnection.setRequestMethod("GET");
与
httpURLConnection.setRequestMethod("POST");