C# 入門 & 実践 / C sharp

311-1. HTTP GET リクエストでデータ取得。


・GET
・POST
・SSL通信
・Cookie
・PHPセッション

の順に見て行きたいと思います。

フォームは以下のようなイメージで作成します。



・テキストボックスにURLを入力するとGETでデータ取得するものを作成します。
    public partial class Form1 : Form
    {
        private string myurl = "";
        private string sp1 = "";
        private string sp2 = "";
        private string sv1 = "";
        private string sv2 = "";

        private HttpWebRequest HttpWReq;
        public Form1()
        {
            InitializeComponent();
        }

        private void doUrl( string method )
        {
            try
            {
                DateTime start = DateTime.Now;


                myurl = my_url.Text;
                if (myurl.Length < 10)
                {
                    //
                    MessageBox.Show("URLを入力してください。");
                    return;
                }

                Encoding enc = Encoding.UTF8;

                HttpWReq = (HttpWebRequest)WebRequest.Create(myurl);
                HttpWReq.Method = method;           // GET or POST
                HttpWReq.UserAgent = "useragent"; // ユーザエージェント
                HttpWReq.ReadWriteTimeout = 5 * 1000; // 読み書き時のタイムアウト?
                HttpWReq.Timeout = 5 * 1000;        // タイムアウト設定
                HttpWReq.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                HttpWReq.KeepAlive = true;



                HttpWReq.ContentType = "application/x-www-form-urlencoded";

                DateTime x1 = DateTime.Now;
                HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                DateTime x2 = DateTime.Now;

                Stream resStream = HttpWResp.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, enc);
                string html = sr.ReadToEnd();
                sr.Close();
                resStream.Close();

                // Insert code that uses the response object.
                HttpWResp.Close();
                

                DateTime end = DateTime.Now;

                double j0 = end.Ticks - start.Ticks;
                double j1 = x1.Ticks - start.Ticks;
                double j2 = x2.Ticks - x1.Ticks;
                double j3 = end.Ticks - end.Ticks;
                j0 = (j0 * 0.000000100);
                j1 = (j1 * 0.000000100);
                j2 = (j2 * 0.000000100);
                j3 = (j3 * 0.000000100);
                //jikan = (double)(jikan * 100/1000000000);
                timecost.Text = "① : " + j1.ToString() + "\n";
                timecost.Text += "② : " + j2.ToString() + "\n";
                timecost.Text += "③ : " + j3.ToString() + "\n";
                timecost.Text += "全 : " + j0.ToString() + "\n";
                timecost.Refresh();
                MessageBox.Show("取得文字数 : " + html.Length.ToString() + "\n\nソースが500文字以内の場合表示します。");
                if (html.Length < 500)
                {
                    MessageBox.Show(html);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        // GETボタンクリック
        private void button1_Click(object sender, EventArgs e)
        {
            doUrl("GET");
        }
とくに注意するところはないのですが
HttpWebRequest
HttpWebResponse
を利用してHTTP通信を行います。

一箇所だけ注意
HttpWReq.ReadWriteTimeout = 5 * 1000; // 読み書き時のタイムアウト?
HttpWReq.Timeout = 5 * 1000; // タイムアウト設定
これで、Timeoutは2種類あるので注意が必要です。



HTTP関連 » 311-2. HTTP GET/POST データを送信!

C# 入門 & 実践 / C sharp