勉強。
とりあえず参考URLコピペ加工。
// 参考 http://www.divakk.co.jp/aoyagi/csharp_winexe_05.html using System; using System.IO; using System.Net; class c { public static void Main() { UrlOpen("http://chrono.s9.xrea.com/"); } private static void UrlOpen(string url) { WebRequest req = WebRequest.Create( url ); WebResponse rsp = req.GetResponse(); Stream stm = rsp.GetResponseStream(); if (stm != null) { StreamReader reader = new StreamReader(stm, System.Text.Encoding.GetEncoding("Shift_JIS")); string readText = reader.ReadToEnd(); Console.WriteLine(readText); stm.Close(); } rsp.Close(); } }ruby
# 参考 http://www.ruby-lang.org/ja/man/html/net_http.html require 'net/http' Net::HTTP.version_1_2 # おまじない Net::HTTP.start("chrono.s9.xrea.com", 80) {|http| response = http.get('/index.html') puts response.body }
C# WebClientを使うともう少し楽そう
1) URLからWebRequestの生成 → WebResponseの取得 → ストリームの取得
2) WebClient生成 → URLからストリーム取得
// 参考 http://dobon.net/vb/dotnet/internet/webclientopenread.html using System; using System.IO; using System.Net; class c { public static void Main() { UrlOpen("http://chrono.s9.xrea.com/"); } private static void UrlOpen(string url) { System.Net.WebClient wc = new System.Net.WebClient(); Stream st = wc.OpenRead( url ); StreamReader sr = new StreamReader(st, System.Text.Encoding.GetEncoding("Shift_JIS")); Console.WriteLine(sr.ReadToEnd()); st.Close(); wc.Dispose(); } }