Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我在创建BeginGetRequestStream时会得到ProtocolViolationException_C#_Silverlight_Windows Phone - Fatal编程技术网

C# 为什么我在创建BeginGetRequestStream时会得到ProtocolViolationException

C# 为什么我在创建BeginGetRequestStream时会得到ProtocolViolationException,c#,silverlight,windows-phone,C#,Silverlight,Windows Phone,我是silverlight的新手。我正在Visual Studio 2010中为Windows phone编程。 我尝试执行HttpWebRequest,但调试器显示ProtocolViolationException。 这是我的密码 private void log_Click(object sender, RoutedEventArgs e) { //auth thi is my url for request string

我是silverlight的新手。我正在Visual Studio 2010中为Windows phone编程。 我尝试执行HttpWebRequest,但调试器显示ProtocolViolationException。 这是我的密码

 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。试试这个:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(auth);
request.Method = "POST";
request.BeginGetRequestStream(RequestCallBack, request);

当您得到请求流时,您甚至没有对它做任何事情

HttpWebRequest
假设您试图获取它的原因是向它写入内容(毕竟,这是获取它的唯一原因)

由于不允许您在GET请求中包含内容,它意识到您只能对该流执行违反HTTP协议的操作。作为一种使用HTTP协议的工具,它的工作就是阻止您犯这种错误

因此,它抛出
ProtocolViolationException


删掉关于请求流的部分-它仅用于POST和PUT。直接转到
GetResponse()
BeginGetResponse()

您忘了清理一个客户端机密/id对。有人能给我HttpWebRequest的工作代码吗?@user1597524那么您不能在流中写入任何内容。在GET请求中包含内容违反了HTTP协议。这违反了协议。我不知道WebRequest会立即抛出该异常(而不是让您在收到响应之前将“POST”、“PUT”等设置为方法),但我肯定会在某个时候看到ProtocolViolationException。