简体   繁体   中英

Error on WP7 app WebRequest

I have this errors on 'error List'.

'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'

I'm make an App WindowsPhone 7 with vb.net on VisualStudio 2010

i can't understand why. Thanks

You can't due to the async nature of WP. It seems like you have lifted examples from a non WP project. In WP, you have to make the call and then register an even that listens to the completion of the task. Further reading on 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);
            }
        }
    }
 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM