开发者

Why does this code run in WinForms but not in ASP.NET?

This piece of code runs perfectly in console app and WinForm application.

I want to use the same logic on an ASP.NET website where the data cannot be retreived using httpwebrequest.

Kindly let me know how to solve this issue. Is there anything better I can do than this. I have marked the line which is causing a problem.

private void runBrowserThread()
{
    var th = new Thread(() =>
    {
        var br = new WebBrowser();
        br.DocumentCompleted += browser_DocumentCompletedNew;
        br.Navigate("https://www.linkedin.com/uas/login");
        Application.Run();    // ERROR ON THIS LINE 
    });
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}

void browser_DocumentCompletedNew(object sender, 
                                  WebBrowserDocumentCompletedEventArgs e)
{
    var br = sender as WebBrowser;
    if (br.Url 开发者_JAVA百科== e.Url)
    {
    }
}

Thank you in advance.


I would suggest using System.Net.WebClient or System.Net.HttpWebRequest for this.

Here's a simple example using HttpWebRequest:

WebRequest request = HttpWebRequest.Create("https://www.linkedin.com/uas/login");
request.Method = WebRequestMethods.Http.Get;

using (WebResponse response = request.GetResponse())
{                
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string responseText = reader.ReadToEnd();
    reader.Close();
}


You have to go about it another way because Application is only valid in the context of a Windows.Form class. Even if it was working, all Application.Run would do is run a thread that does nothing.

I strongly suggest you keep this a desktop application, what you are trying to do, is not really suited for a web application.

If you want help you must provide us with more information. The code you have at this point prety much does nothing of any substance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜