我遇到了一些代码访问宁静的Web服务的问题。运行此代码时,它在var httpResponse =(HttpWebResponse)httpWebRequest.GetResponse()处出错;并且返回的异常是:“System.Net.WebException:远程服务器返回错误:(415)UNSUPPORTED MEDIA TYPE。”System.Net.WebException:远程服务器返回一个错误:(415)UNSUPPORTED MEDIA TYPE
public bool CreateAccount(string myUsername, string url, string authtoken) {
try {
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.MediaType="application/json";
httpWebRequest.Accept="application/json";
httpWebRequest.Method = "POST";
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("Authorization: Token"+authtoken);
httpWebRequest.Headers = headers;
using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
streamWriter.Write("{username : '"+myUsername+"'}");
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); // Fails on this line.
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string json = streamReader.ReadToEnd();
}
return true;
} catch (WebException e) {
throw e;
return false;
}
//return true;
}
我曾尝试过各种东西contentType中,的MediaType和接受,但是工作实例由服务供应-H“的ContentType:应用/ JSON的”开发商给我的卷曲,所以看起来“application/json”是正确的值。他也做--data-binary,我认为streamWriter会为我做。
有谁知道什么可能会导致此错误?
2013-05-31
petFoo