« PG_2008夏 | メイン | [PG] http get proxy »

[PG] http get

勉強。
とりあえず参考URLコピペ加工。

C#
// 参考 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();
	}
}

トラックバック

このエントリーのトラックバックURL:
http://chrono.s9.xrea.com/x/daysMT/mt-tb.cgi/264

コメントを投稿

About

2008年08月03日 01:19に投稿されたエントリーのページです。

ひとつ前の投稿は「PG_2008夏」です。

次の投稿は「[PG] http get proxy」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。

Powered by
Movable Type 3.34