AsyncHttpWebRequest(网页标题检索程序)

发布于 2024-12-22 09:42:18 字数 1109 浏览 1 评论 0原文

我正在尝试创建一个程序,该程序将检索给定网址的页面标题。我编写的代码在不使用 AsyncCallback 时可以工作,但是当我使用 AsyncCallback 时,代码似乎不起作用。

public void GetWebPageTitle(string URL)
{
    // make request for web page
    HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
    myWebRequest.Method = "GET";
    myWebRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), myWebRequest);
    zConsole.WriteLine("Beginning HttpWebRequest for: " + URL);
}

void FinishWebRequest(IAsyncResult result)
{
    zConsole.WriteLine("...");
    string title = "Unknown";

    //Code under here doesnt get extcuted

    HttpWebResponse myWebResponse = (HttpWebResponse)((HttpWebRequest)result.AsyncState).EndGetResponse(result);
    StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
    string source = "";            
    source = myWebSource.ReadToEnd();
    myWebResponse.Close();
    title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
    zConsole.WriteLine(title);
}

谢谢。

I'm trying to create a program that will retrieve page titles given a url. I've written code that works when I'm not using a AsyncCallback, but when I use a AsyncCallback the code doesn't seem to work.

public void GetWebPageTitle(string URL)
{
    // make request for web page
    HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
    myWebRequest.Method = "GET";
    myWebRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), myWebRequest);
    zConsole.WriteLine("Beginning HttpWebRequest for: " + URL);
}

void FinishWebRequest(IAsyncResult result)
{
    zConsole.WriteLine("...");
    string title = "Unknown";

    //Code under here doesnt get extcuted

    HttpWebResponse myWebResponse = (HttpWebResponse)((HttpWebRequest)result.AsyncState).EndGetResponse(result);
    StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
    string source = "";            
    source = myWebSource.ReadToEnd();
    myWebResponse.Close();
    title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
    zConsole.WriteLine(title);
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2024-12-29 09:42:18

我认为,问题是,您的程序在返回异步结果之前结束。
执行 Console.Writeline 后主线程死亡。

休息看起来还不错。 BeginGetResponse 位于 MSDN

I think, the problem is, your program ends, before async result is returned.
The main thread after doing Console.Writeline dies.

Rest looks okay. BeginGetResponse at MSDN

雨的味道风的声音 2024-12-29 09:42:18

在回调内的代码周围放置一个 try/catch 块,看看其中是否有任何内容抛出异常。

否则更多细节会很有用。当您说代码没有被执行时,您实际上是单步执行代码/使用断点,还是您根据控制台输出假设是这种情况?此请求是从应用程序的主窗口线程发出的吗?

Put a try/catch block around the code inside the callback and see if anything in there is throwing an exception.

Otherwise some more details would be useful. When you say that the code doesn't get executed are you actually stepping through the code/using breakpoints or are you assuming this is the case based on your console output? Is this request being made from the main window thread of your application?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文