【问题标题】:Android Soap request is getting error using HttpUrlConnectionAndroid Soap 请求使用 HttpUrlConnection 出错
【发布时间】:2017-07-15 17:33:40
【问题描述】:

我使用 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 请求如下

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>

【问题讨论】:

    标签: android web-services soap httpurlconnection


    【解决方案1】:

    你有没有尝试改变 httpURLConnection.setRequestMethod("GET");

    httpURLConnection.setRequestMethod("POST");

    【讨论】:

    • 感谢您的回答@yk Order,这只是 xml 字符串中的错字。我复制了整个字符串并粘贴,工作
    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多