▼.Net Compact Framework 2.0メモ▼
HTTP通信


ネット上のテキストファイルを読み込んで表示するプログラムを作成する。



デザインの編集
HttpForm
プロパティ
Text HttpEx
MinimizeBox false
FormFactor Windows Mobile 6 Professional VGA


TextBox - tbText
プロパティ
Text (なし)
Location 20,40
Size 400,41

Button - btnRead
プロパティ
Text 通信
Location 20,100
イベント メソッド
Click btnRead_Click


ソースコードの編集
HttpForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace HttpEx {
    public partial class HttpForm : Form {
        //コンストラクタ
        public HttpForm() {
            InitializeComponent();
        }

        //読み込みボタンのイベント処理
        private void btnRead_Click(object sender, EventArgs e) {
            try {
                tbText.Text=LoadText("http://npaka.net/HttpEx/test.txt");
            } catch (Exception exc) {
                MessageBox.Show(exc.ToString(),"エラー");
            }
        }

        //テキストの読み込み
        private String LoadText(String url) {
            HttpWebRequest req=WebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse res=req.GetResponse() as HttpWebResponse;
            StreamReader reader=new StreamReader(res.GetResponseStream(),
                Encoding.GetEncoding("Shift_JIS"));
            String result=reader.ReadToEnd();
            reader.Close();
            return result;
        }
    }
}



−戻る−