WP7应用程序WebRequest出错

时间:2013-10-10 13:39:37

标签: vb.net windows-phone-7 webrequest

我在'错误列表'上有这个错误。

'ContentLength' is not a member of 'System.Net.WebRequest'

'GetRequestStream' is not a member of 'System.Net.WebRequest'

'GetResponse' is not a member of 'System.Net.WebRequest'

我在VisualStudio 2010上使用vb.net制作应用程序WindowsPhone 7

我无法理解为什么。感谢

2 个答案:

答案 0 :(得分:1)

你不能因为WP的异步性质。看起来你已经取消了非WP项目的例子。在WP中,您必须进行调用,然后注册一个侦听任务完成的偶数。进一步阅读Async Calls

private void GetSource(object sender, RoutedEventArgs e)
    {
        System.Net.WebRequest request = WebRequest.Create("http://www.bbc.co.uk");
        //request.ContentType = "application/json";
        request.Method = "GET";

        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

 private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        var request = asynchronousResult.AsyncState as HttpWebRequest;

        if (request != null)
        {
            try
            {
                WebResponse response = request.EndGetResponse(asynchronousResult);
                using (Stream stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        var responseString = reader.ReadToEnd();

                        MessageBox.Show(responseString);
                    }                      
                }
            }
            catch (WebException e)
            {
               // Handle exception
                MessageBox.Show(e.Message);
            }
        }
    }

答案 1 :(得分:0)

 public void SendPost(Uri uri, string json)
    {
    var webClient = new WebClient();
    webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
    webClient.UploadStringCompleted += this.sendPostCompleted;
    webClient.UploadStringAsync(uri, "POST", json);
    }