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);
您是否嘗試使用Postman或其他類似工具發送POST消息?我聽起來你正在發佈到服務的其他端點(並且該端點沒有允許POST方法) – Hudvoy
'wr.close();'。刪除。它也會關閉插座。 – greenapps