我不知道。我正在尝试从我正在为Windows Phone构建的应用程序上的REST服务中获取XML。我总是在以下行中得到例外:
HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
我有以下设置(忽略坏网址,这只是一个例子..)
HttpWebRequest request = WebRequest.Create("https://domain.chargify.com/customers.xml") as HttpWebRequest;
NetworkCredential credentials = new NetworkCredential("appkeyhere", "password");
request.Credentials = credentials;
request.Method = "GET";
request.ContentType = "text/xml";
request.BeginGetResponse(new AsyncCallback(SomeCallback), request);
...
private void SomeCallback(IAsyncResult ar) {
HttpWebRequest request = ar.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
XElement xmlResult = XElement.Parse(reader.ReadToEnd());
...
}
例外情况如下:
System.Net.ProtocolViolationException was unhandled
Message=ProtocolViolationException
StackTrace:
at System.Net.Browser.ClientHttpWebRequest.PrepareAndSendRequest(String method, Uri requestUri, Stream requestBodyStream, WebHeaderCollection headerCollection, CookieContainer cookieContainer)
at System.Net.Browser.ClientHttpWebRequest.BeginGetResponseImplementation()
at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
at System.Net.Browser.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)
at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at ChargifyWPA.MainPage.button1_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
有人有什么想法吗?它是https,而不是http ..是导致问题的原因吗? 感谢。
答案 0 :(得分:15)
这不是你的问题,但可能有助于其他人。我碰到了这个,因为我在GET请求上调用了BeginGetRequestStream()。当然,这不是必要的,我应该刚刚调用BeginGetResponse()。愚蠢的错误,但如果错误消息更有帮助,我会更快地弄清楚它。
答案 1 :(得分:8)
我敢打赌这个问题是因为您设置了'ContentType'标题,但未在请求中提供内容。请注意,尝试创建请求时会抛出协议异常。
您可能需要将“Accept”标题设置为“text / xml”而不是内容类型标题。
答案 2 :(得分:1)
带有“GET”的“Content-Type”标头会导致请求中出现异常 在Azure Table REST API中,需要将ContentType编码为auth标头 - 对GET标头的“ContentType”使用空字符串
答案 3 :(得分:0)
显然,最近桌面上出现此错误的最常见原因是需要SSL而不是TLS身份验证的Web服务器。查看有关this article explaining troubleshooting options for various .NET 2 HTTP errors的所有评论。您在CodePlex上的MIT许可的Chargify.NET项目似乎已经using this work-around(“SecurityProtocolType.Ssl3”)。
从another article circa 2008尝试的其他选项会回退到HTTP / 1.0或禁用Keep-Alive。
请务必尝试this answer,删除ContentType初始化。