0

Does anyone know how to acess a url from a windows application?.

I have an address http://serverport/Page.I want to acess this page from my windows application.

Regards, Harsh Suman

4

3 回答 3

3

It's not clear what you want to do with the page.

If you want to display it on the form, you can use a WebBrowser control.

If you want to get the response and process it, use the System.Net.WebClient class.

于 2008-12-10T07:27:21.033 回答
1

如果要下载 HTML 或任何文件,可以使用 WebClient 类。

例子:

    /// <summary>
    /// Downloads a file from the given location
    /// </summary>
    /// <param name="url">Location of the file</param>
    /// <param name="dest">The destination of the downloaded file</param>
    /// <returns>False if there was an error, else True</returns>
    public bool DownLoad(string url, string dest)
    {
        WebClient client = new WebClient();
        try
        {
            //Downloads the file from the given url to the given destination                
            client.DownloadFile(url, dest);
            return true;
        }
        catch (WebException)
        {
            // Handle exception
            return false;
        }
        catch (System.Security.SecurityException)
        {
            // Handle exception
            return false;
        }
        catch (Exception)
        {
            // Handle exception
            return false;
        }
    }
于 2008-12-10T08:22:22.590 回答
0

我不确定你在问什么,所以我只是给出了另一种解释问题的方式的答案。

如果您只是想启动默认浏览器(显示本地或在线 html 手册等),在 Windows 中(在其他操作系统中可能类似),您可以使用某种“执行界面”来执行格式正确的 url作为命令,这通常会启动默认浏览器:

根据此页面,此代码应启动浏览器:

string targeturl= "http://stackoverflow.com";

try
    {
     System.Diagnostics.Process.Start(targeturl);
    }
catch
    ( 
     System.ComponentModel.Win32Exception noBrowser) 
    {
     if (noBrowser.ErrorCode==-2147467259)
      MessageBox.Show(noBrowser.Message);
    }
catch (System.Exception other)
    {
      MessageBox.Show(other.Message);
    }

(不过,错误代码的幻数看起来很丑陋……)

于 2008-12-10T07:53:28.327 回答