Windows アプリケーションから URL にアクセスする方法を知っている人はいますか?
アドレスはhttp://serverport/Pageです。Windows アプリケーションからこのページにアクセスしたいと考えています。
よろしく、 厳しいスーマン
Windows アプリケーションから URL にアクセスする方法を知っている人はいますか?
アドレスはhttp://serverport/Pageです。Windows アプリケーションからこのページにアクセスしたいと考えています。
よろしく、 厳しいスーマン
ページで何をしたいのか明確ではありません。
フォームに表示したい場合は、WebBrowser
コントロールを使用できます。
応答を取得して処理する場合は、System.Net.WebClient
クラスを使用します。
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;
}
}
あなたが何を求めているのかわからないので、質問を解釈するためのさらに別の方法に答えます。
単純にデフォルトのブラウザーを起動したい場合 (ローカルまたはオンラインの 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);
}
(エラー コードのマジック ナンバーではかなり見栄えが悪いですが...)