Question

I am writing a phone app in C# / .xaml

Instead of simply directing the app to a website I want to pull certain information from a specific website. I know the class name that I want the information from too.

I have code but the request.BeginGetResponse(); returns an error 'No overload for method'. What am I doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GamesWithGold.Resources;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace MyIP
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        class WebFetch
        {
            static void Main(string[] args)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com/");
                HttpWebResponse response = (HttpWebResponse)request.BeginGetResponse();

                StreamReader stream = new StreamReader(response.GetResponseStream());

                string final_response = stream.ReadToEnd();

                Regex r = new Regex(@"Your IP");
                Match m = r.Match(final_response);

                Console.WriteLine(final_response);
            }
        }
    }
}
Était-ce utile?

La solution 2

I think that you want to call just GetResponse instead of BeginGetResponse. BeginGetResponse if for invoking GetResponse asynchronously. Your code should look like:

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Autres conseils

The error basically means that the signature of the method you used in the sample above doesn't match with the allowed signature of the BeginGetResponse method. The correct way to implement this method would be to pass in a callback and a request state. The callback will be your handler which will be executed once the request returns with a response.

Please see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx for understanding on how to correctly implement this method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top