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