C# exercises (c) Web3 HttpClient

Visual Studio community 2015 アカウントについて (学内専用)

  1. WebBrowser (基本)
  2. Form(自動操作)
  3. HttpClient ←今週
  4. REST & JSON

いままで、WebBrowser コントロールを利用して、 Webサイトを表示と操作プログラムを作りました。

今回は直接 http 通信に HttpClient クラスを使用 するプログラムを作ります。

HTTPとは

httpとは、代表的な通信プロトコルの一つで、インターネット上でWebサーバー(サイトの公開側)と、クライアント(ブラウザ:サイトを閲覧する側)が、相互に通信するために使用される仕組みです。

もっと言うと、Webサーバーとクライアント間で、HTML(Webページを作成するための言語)で記載された情報をやりとりするための仕組みです。

この「http」がないと、インターネット上のサイトを見ることができなくなるので、今や無くてはならない仕組みとなります。

r10zu01-2

HTTPコマンド

主なHTTPコマンドには次のようなものがあります。

GET 指定したページの取得を要求します。
HEAD メッセージヘッダを要求します。
POST フォームに入力したデータを送る
PUT サーバーへデータをアップロードする
DELETE サーバー上のデータを削除する

 

HTTPコマンド応答メッセージ

主な応答メッセージには、次のようなものがあります。

200 OK 正常に取得できた
301 恒久的に移転した
302 一時的に移転した
403 Forbidden アクセス拒否
404 Not Found ファイルが見つからない

 

HttpClient 通信プログラム

デザイン

web_get

 

  • Form
    • Form1
  • TextBox
    • textBox1  —
      • Name : URL
      • Text : http://wp-api.xyz
    • textBox2  —
      • Name :  ProxyID
      • Text : (your_proxy_id)
    • textBox3  —
      • Name :  ProxyPW
      • PasswordChar : *
      • Text : (your_proxy_password)
    • textBox4  —
      • Name : HTTP_Results
      • Multiline : True
  • Button
    • button1 —
      • Name : Quit
    • button2 —
      • Name : Request
  • CheckBox
    • checkBox1 —
      • Name : ProxyOn

 

プログラム

HttpClient クラス

// for httpclient
using System.Net.Http;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
// using System.Runtime.Serialization.Json;

Quit ボタン

        private void Quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

Requestボタン

private async void Request_Click(object sender, EventArgs e)
{
    if (ProxyOn.Checked)
    {
        // デフォルト プロキシ情報を取得して、資格情報を設定する 
        var proxy = WebRequest.DefaultWebProxy;
        proxy.Credentials = new NetworkCredential(ProxyID.Text, ProxyPW.Text);
    }
    var client = new HttpClient();
    string response = await client.GetStringAsync(URL.Text);
    HTTP_Results.Text = response;

}

最終プログラム:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// for httpclient
using System.Net.Http;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
// using System.Runtime.Serialization.Json;


namespace web_get2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private async void Request_Click(object sender, EventArgs e)
        {
            if (ProxyOn.Checked)
            {
                // デフォルト プロキシ情報を取得して、資格情報を設定する 
                var proxy = WebRequest.DefaultWebProxy;
                proxy.Credentials = new NetworkCredential(ProxyID.Text, ProxyPW.Text);
            }
            var client = new HttpClient();
            string response = await client.GetStringAsync(URL.Text);
            HTTP_Results.Text = response;

        }
    }
}

実行結果

web_get2

 

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Related Post

C# exercises (5) CalculatorC# exercises (5) Calculator

my電卓を作ろう 逆ポーランド記法電卓 Reverse Polish Notation Calculator 画面をデザインする ツールボックスの中で、 コモンコントロール「TextBox」 TextBox –>  (name): typeText;  text: 0 TextBox –> (name): lastText;  text: 0 コモンコントロール「Button」 […]

C# exercises (1) GuidanceC# exercises (1) Guidance

Windowsアプリを作る C#について 開発しやすく,高機能なプログラミング言語 様々なプログラミング言語の良いところを組合せて作った. プログラムのミス(バグ)が出にくい 学習しやすい 比較的簡単に自分が作成したいWindowsアプリを作れる Windowsアプリの開発ではC#を使うことが標準になってきている。 この課程について Microsoft Visual C# 2015利用 とても難しいWindowsアプリの開発をできるだけ簡単にして、いろいろな高度な機能を教えたい。 少しでもプログラミングの楽しさが伝わるように工夫したい。 演習中心:できるだけ見やすく工夫します。 簡単ながら、応用しやすく、かつ、興味がもてるような題材を考えてきます。 試験:レポート 評価:出席+演習+レポート *** […]

C# exercises (7) Puzzle GameC# exercises (7) Puzzle Game

今回作るのは、イメージを読み込んで利用するパズルゲームです。イメージファイルを読み込むとそれを 9 分割し、ランダムに混ぜます。 プレビューのイメージをフォーム上クリックして配置、すべて正しい場所に配置できればクリアです。   フォーム作成 サイズ:500 x 400 背景色:適当 PictureBox配置 (Name) : PlayBox Size :        300 x 300 BackColor […]