本文介绍了为什么在执行 BeginGetRequestStream 时会收到 ProtocolViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Silverlight 的新手.我在 Visual Studio 2010 中为 Windows 手机编程.我尝试执行 HttpWebRequest,但调试器显示 ProtocolViolationException.这是我的代码
I am new to silverlight. I am programming in Visual Studio 2010 for Windows phone.I try to do HttpWebRequest but debugger says ProtocolViolationException.This my code
private void log_Click(object sender, RoutedEventArgs e)
{
//auth thi is my url for request
string auth;
string login = Uri.EscapeUriString(this.login.Text);
string password = Uri.EscapeUriString(this.pass.Password);
auth = "https://api.vk.com/oauth/token";
auth += "?grant_type=password" + "&client_id=*****&client_secret=******&username=" + login + "&password=" + password + "&scope=notify,friends,messages";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(auth);
request.BeginGetRequestStream(RequestCallBack, request);//on this line debager say ProtocolViolationExceptio
}
void RequestCallBack(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
Stream stream = request.EndGetRequestStream(result);
request.BeginGetResponse(ResponceCallBack, request);
}
void ResponceCallBack(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string a =sr.ReadToEnd();
MessageBox.Show(a);
}
}
推荐答案
我认为问题在于您使用的不是 POST,而是 GET.试试这个:
I think the problem is that you aren't using POST, but GET. Try this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(auth);
request.Method = "POST";
request.BeginGetRequestStream(RequestCallBack, request);
这篇关于为什么在执行 BeginGetRequestStream 时会收到 ProtocolViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!