0

Windows アプリケーションから URL にアクセスする方法を知っている人はいますか?

アドレスはhttp://serverport/Pageです。Windows アプリケーションからこのページにアクセスしたいと考えています。

よろしく、 厳しいスーマン

4

3 に答える 3

3

ページで何をしたいのか明確ではありません。

フォームに表示したい場合は、WebBrowserコントロールを使用できます。

応答を取得して処理する場合は、System.Net.WebClientクラスを使用します。

于 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 で (おそらく他の OS でも同様です)、ある種の「実行インターフェース」を使用して、正しくフォーマットされた 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 に答える