cscの作法 その167

# 概要
cscの作法、調べてみた。
ChromeDevToolsProtocol、叩いてみた。

# 環境

windows 10 64bit
chrome 101.0.4951.67

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/b768981f-ae32-3c6b-9ead-0aeff23bd35e.png)


# サンプルコード

```

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Web;
using System.IO;
using System.Xml;

public class Form1 : Form {
	Button button0,
		button1,
		button2,
		button3,
		button4;
	public Form1() {
		this.ClientSize = new Size(400, 400);
		this.Text = "cdp";
		button0 = new Button();
		button0.Location = new Point(10, 10);
		button0.Size = new Size(80, 30);
		button0.Text = "0";
		button1 = new Button();
		button1.Location = new Point(110, 10);
		button1.Size = new Size(80, 30);
		button1.Text = "1";
		button2 = new Button();
		button2.Location = new Point(210, 10);
		button2.Size = new Size(80, 30);
		button2.Text = "2";
		button3 = new Button();
		button3.Location = new Point(10, 60);
		button3.Size = new Size(80, 30);
		button3.Text = "3";
		button4 = new Button();
		button4.Location = new Point(110, 60);
		button4.Size = new Size(80, 30);
		button4.Text = "4";
		Controls.Add(button0);
		Controls.Add(button1);
		Controls.Add(button2);
		Controls.Add(button3);
		Controls.Add(button4);
		button0.Click += Button0_Click;
		button1.Click += Button1_Click;
		button2.Click += Button2_Click;
		button3.Click += Button3_Click;
		button4.Click += Button4_Click;
	}
	private void Button0_Click(object sender, EventArgs e) {
		using (var process = new Process()) 
		{
			process.StartInfo = new ProcessStartInfo {
				FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
				Arguments = @"--remote-debugging-Port=9222",
				CreateNoWindow = true,
				UseShellExecute = false,
			};
			process.Start();
		}
	}
	private void Button1_Click(object sender, EventArgs e) {
		using (WebClient client = new WebClient())
		{
			client.DownloadDataCompleted += (_, args) => {
				var headers = client.ResponseHeaders;
				string s = "";
				for (int i = 0; i < headers.Count; ++i)
				{
					string header = headers.GetKey(i);
					foreach (string value in headers.GetValues(i))
					{
						s += string.Format("{0}: {1}\r\n", header, value);
					}
				}
				Console.WriteLine(s);
				var bytes = args.Result;
				var html = Encoding.UTF8.GetString(bytes);
				Console.WriteLine(html);
			};
			client.DownloadDataAsync(new Uri("http://localhost:9222/json"));
		}
	}
	private void Button2_Click(object sender, EventArgs e) {
		WebRequest request = WebRequest.Create("http://localhost:9222/json");
		WebResponse response = request.GetResponse();
		Console.WriteLine(((HttpWebResponse) response).StatusDescription);
		using (Stream dataStream = response.GetResponseStream())
		{
			StreamReader reader = new StreamReader(dataStream);
			string responseFromServer = reader.ReadToEnd();
			Console.WriteLine(responseFromServer);
			JsonData jsonData = JsonMapper.ToObject(responseFromServer);
			Console.WriteLine(jsonData[0]["id"]);
			Console.WriteLine(jsonData[1]["id"]);
			Console.WriteLine(jsonData[2]["id"]);

		}
		response.Close();
	}
	private void Button3_Click(object sender, EventArgs e) {
	}
	private void Button4_Click(object sender, EventArgs e) {
	}
	[STAThread]
	public static void Main() {
		Application.Run(new Form1());
	}
}





```

# コンパイル手順

```
>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%



>csc chrome0.cs /reference:System.Net.Http.dll


```

# 結果

```
OK
[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/7919A6579AA16446DA64D0DC8A653287",
   "id": "7919A6579AA16446DA64D0DC8A653287",
   "title": "新しいタブ",
   "type": "page",
   "url": "chrome://newtab/",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/7919A6579AA16446DA64D0DC8A653287"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/53A89F3AF6DDCDC4F670D8F1097EDF12",
   "id": "53A89F3AF6DDCDC4F670D8F1097EDF12",
   "parentId": "7919A6579AA16446DA64D0DC8A653287",
   "title": "chrome-untrusted://new-tab-page/one-google-bar?paramsencoded=",
   "type": "iframe",
   "url": "chrome-untrusted://new-tab-page/one-google-bar?paramsencoded=",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/53A89F3AF6DDCDC4F670D8F1097EDF12"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/37EE93C7A3A8CB8C213E1F933D2970EF",
   "id": "37EE93C7A3A8CB8C213E1F933D2970EF",
   "parentId": "53A89F3AF6DDCDC4F670D8F1097EDF12",
   "title": "https://ogs.google.com/u/0/widget/app?origin=chrome-untrusted%3A%2F%2Fnew-tab-page&amp;origin=chrome%3A%2F%2Fnew-tab-page&amp;cn=app&amp;pid=1&amp;spid=243&amp;hl=ja",
   "type": "iframe",
   "url": "https://ogs.google.com/u/0/widget/app?origin=chrome-untrusted%3A%2F%2Fnew-tab-page&origin=chrome%3A%2F%2Fnew-tab-page&cn=app&pid=1&spid=243&hl=ja",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/37EE93C7A3A8CB8C213E1F933D2970EF"
} ]

7919A6579AA16446DA64D0DC8A653287
53A89F3AF6DDCDC4F670D8F1097EDF12
37EE93C7A3A8CB8C213E1F933D2970EF
Content-Length: 1607
Content-Type: application/json; charset=UTF-8

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/7919A6579AA16446DA64D0DC8A653287",
   "id": "7919A6579AA16446DA64D0DC8A653287",
   "title": "新しいタブ",
   "type": "page",
   "url": "chrome://newtab/",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/7919A6579AA16446DA64D0DC8A653287"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/53A89F3AF6DDCDC4F670D8F1097EDF12",
   "id": "53A89F3AF6DDCDC4F670D8F1097EDF12",
   "parentId": "7919A6579AA16446DA64D0DC8A653287",
   "title": "chrome-untrusted://new-tab-page/one-google-bar?paramsencoded=",
   "type": "iframe",
   "url": "chrome-untrusted://new-tab-page/one-google-bar?paramsencoded=",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/53A89F3AF6DDCDC4F670D8F1097EDF12"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/37EE93C7A3A8CB8C213E1F933D2970EF",
   "id": "37EE93C7A3A8CB8C213E1F933D2970EF",
   "parentId": "53A89F3AF6DDCDC4F670D8F1097EDF12",
   "title": "https://ogs.google.com/u/0/widget/app?origin=chrome-untrusted%3A%2F%2Fnew-tab-page&amp;origin=chrome%3A%2F%2Fnew-tab-page&amp;cn=app&amp;pid=1&amp;spid=243&amp;hl=ja",
   "type": "iframe",
   "url": "https://ogs.google.com/u/0/widget/app?origin=chrome-untrusted%3A%2F%2Fnew-tab-page&origin=chrome%3A%2F%2Fnew-tab-page&cn=app&pid=1&spid=243&hl=ja",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/37EE93C7A3A8CB8C213E1F933D2970EF"
} ]







```
以上。