2017-07-15 74 views
0

我使用HttpUrlconnection類作爲http請求發送Soap請求,但得到了405錯誤。 我想知道什麼,我在發送HTTP POST請求的正確與否 ,我不想使用任何庫或API類似KSOAP等...Android Soap請求使用HttpUrlConnection獲取錯誤

@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

你試圖改變 httpURLConnection.setRequestMethod("GET");

httpURLConnection.setRequestMethod("POST");

+0

這不會提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18654374) – Sunil