C# HttpWebRequest错误-request.BeginGetResponse();
我正在用C#/.xaml编写一个手机应用程序 我不想简单地将应用程序指向某个网站,而是想从某个特定网站获取某些信息。我也知道我想从中获取信息的类名 我有代码,但没有请求。BeginGetResponse();返回错误“方法没有重载”。我做错了什么C# HttpWebRequest错误-request.BeginGetResponse();,c#,xaml,windows-phone-8,httpwebrequest,C#,Xaml,Windows Phone 8,Httpwebrequest,我正在用C#/.xaml编写一个手机应用程序 我不想简单地将应用程序指向某个网站,而是想从某个特定网站获取某些信息。我也知道我想从中获取信息的类名 我有代码,但没有请求。BeginGetResponse();返回错误“方法没有重载”。我做错了什么 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Window
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);
}
}
}
}
我认为您只需要调用
GetResponse
,而不是BeginGetResponse
BeginGetResponse
if用于异步调用GetResponse
。您的代码应该如下所示:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
该错误基本上意味着您在上述示例中使用的方法的签名与BeginGetResponse方法允许的签名不匹配。实现此方法的正确方法是传入回调和请求状态。回调将是您的处理程序,一旦请求返回响应,就会执行回调 请参阅以了解如何正确实施此方法