2017-04-01 147 views
0

我嘗試將HTTP Post請求從Android應用程序發送到C#RESTful Web服務。 由於某種原因,一旦連接,我就會收到響應代碼405。錯誤405從Android向C#Webservice發送HTTP Post請求

如果我嘗試使用我製作的C#客戶端發送POST請求,代碼將正常工作,並且我會成功響應。

於Android(在服務運行ofcourse)代碼:

String url = "http://myurl.com/Service1.svc/login/2"; 
String postDataParams = "{"username":"Ryrud","password":"Pups"}"; 

//Connect 
      httpcon = (HttpURLConnection) ((new URL(url).openConnection())); 
      int responseCode = httpcon.getResponseCode(); 
      Log.d("zzz", "RCode: " + responseCode); // Getting in log: "RCode: 405" 
      httpcon.setDoOutput(true); 
      httpcon.setRequestMethod("POST"); 
      httpcon.setRequestProperty("Content-Type", "application/json"); 
      httpcon.connect(); 

//Post data 
       DataOutputStream wr = new DataOutputStream(httpcon.getOutputStream()); 
       wr.writeBytes(postDataParams); 
       wr.flush(); 
       wr.close(); 

RESTful Web服務代碼:

[OperationContract] 
     [WebInvoke(Method = "POST", 
      ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json, 
      UriTemplate = "login/{id}")] 
      login login(string id, login userpass); 
+0

您是否嘗試使用Postman或其他類似工具發送POST消息?我聽起來你正在發佈到服務的其他端點(並且該端點沒有允許POST方法) – Hudvoy

+0

'wr.close();'。刪除。它也會關閉插座。 – greenapps

回答

0

不要問一個響應代碼,你將數據寫入到的OutputStream之前。

+0

我知道我必須要求後的響應代碼,事情是,結果是相同的,所以我試圖看到,如果id獲得一個不同的,只要我打開連接發生任何一種方式。 – Moshez