プログラム言語 HTTPクライアント

GET

using System.Net;
using System.IO;

リクエスト作成
var req = WebRequest.Create(@"http://officeyone.s324.xrea.com/officeyone.shop");
リクエスト送信&レスポンス取得
var res = req.GetResponse();
↑ 受信データからStreamを取得
Stream resStream = res.GetResponseStream();
読み込み
var reader = new StreamReader(st, Encoding.GetEncoding("Shift_JIS"));
string html = reader.ReadToEnd();
reader.Close();
st.Close();

POST

using System.Net;
using System.IO;

WebRequestの作成
var req = WebRequest.Create("http://~;");
POST送信する文字列をバイト型配列に変換
byte[] postDataBytes = Encoding.ASCII.GetBytes("AAA");
メソッドにPOSTを指定
req.Method = "POST";
ContentTypeを"application/x-www-form-urlencoded"に
req.ContentType = "application/x-www-form-urlencoded";
POST送信するデータの長さを指定
req.ContentLength = postDataBytes.Length;
POST送信の為のStreamを取得
Stream reqStream = req.GetRequestStream();
送信するデータ書き込み
reqStream.Write(postDataBytes, 0, postDataBytes.Length);
reqStream.Close();
リクエストメッセージ取得
var res = req.GetResponse();
↑ 受信データからStreamを取得
Stream resStream = res.GetResponseStream();
読み込み
StreamReader sr = new StreamReader(resStream, Encoding.GetEncoding("shift_jis"));
string html = reader.ReadToEnd();
reader.Close();
st.Close();

HTML取得

using System.Net;
using System.IO;
指定のURLデータを取得
Stream st = new WebClient().OpenRead("http://officeyone.s324.xrea.com/officeyone.shop");
var sr = new StreamReader(st, Encoding.GetEncoding("Shift_JIS"));
string html = sr.ReadToEnd();
sr.Close();
st.Close();
MessageBox.Show(html);

クッキー送受信

DownloadFile

using System.Net;
指定のURLデータを取得&保存
new WebClient().DownloadFile(address: @"http://officeyone.s324.xrea.com/officeyone.shop", fileName: @"office-yone.html");

DownloadData

using System.Net;
指定のURLデータを取得
byte[] data = new WebClient().DownloadData("http://officeyone.s324.xrea.com/officeyone.shop");
string html = Encoding.GetEncoding("Shift_JIS").GetString(data);
MessageBox.Show(html);

プロキシ利用

using System.Net;
var client = new WebClient();
プロキシ利用
client.Proxy = WebRequest.DefaultWebProxy;
プロキシ指定
client.Proxy = new WebProxy("http:~:8080");
プロキシ未使用
client.Proxy = null;
client.~

セキュリティ

$conn = curl_init();

// 取得するURLを指定
$url = "http://office-yone.com/";
curl_setopt ( $conn, CURLOPT_URL , $url );

詳細な情報を出力。STDERRか、またはCURLOPT_STDERRで指定したファイルに出力される
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

サーバー証明書の検証を行わない
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

1:SSLピア証明書に一般名が存在するかどうかを調べる
2:1に加え、その名前がホスト名と一致することを検証する※規定値
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

取得結果を加工しない
curl_setopt($conn, CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec ( $conn );
curl_close($conn);

ブラウザ利用

import webbrowser as brows

ブラウザで指定URLを開く
brows.open('https://office-yone.com/web_access')

Webスクレイピング

import requests as req
import bs4

res = req.get('https://office-yone.com/web_access/')

soup = bs4.BeautifulSoup(res.text)

type(soup)
<class 'bs4.BeautifulSoup'>

elm = soup.select('h5')
type(elm)
<class 'list'>

for item in elm:
  item
  <h5>GET</h5>
  <h5>POST</h5>