C# 入門 & 実践 / C sharp

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


ここでは、パラメータをつけてデータを送信してみたいと思います。

データのつけ方ですが、ご存知の通り

GETの場合は、?xxx のようにURLにつけます。

POSTの場合は、
Stream paraStream = HttpWReq.GetRequestStream();
を利用してパラメータをストリームに書き込みます。

あとは、
HttpUtility.UrlEncode
を利用して、日本語なども渡せるようにしておきます。

    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;
                }
                string param = "";
                sp1 = p1.Text;
                sp2 = p2.Text;
                sv1 = v1.Text;
                sv2 = v2.Text;
                Encoding enc = Encoding.UTF8;
                if (sp1.Length > 1)
                {
                    param = sp1 + "=" + HttpUtility.UrlEncode(sv1, enc);
                }
                if (sp2.Length > 1)
                {
                    if (param.Length > 0)
                    {
                        param += "&";
                    }
                    param += sp2 + "=" + HttpUtility.UrlEncode(sv2, enc);
                }

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



                //HttpWReq.Proxy = WebProxy.GetDefaultProxy();


                HttpWReq.ContentType = "application/x-www-form-urlencoded";
                if (method == "POST")
                {
                    // パラメータ
                    HttpWReq.ContentLength = param.Length;
                    Stream paraStream = HttpWReq.GetRequestStream();
                    StreamWriter sw = new StreamWriter(paraStream);
                    sw.Write(param);
                    sw.Close();
                }


                DateTime x1 = DateTime.Now;
                HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                DateTime x2 = DateTime.Now;
                // string myheader = HttpWResp.GetResponseHeader();
                // MessageBox.Show(myheader);

                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);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doUrl("GET");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            doUrl("POST");
        }



311-1. HTTP GET リクエストでデータ取得。 « HTTP関連 » 311-3. HTTP HTTS/SSL データ通信。


C# 入門 & 実践 / C sharp