強火で進め

このブログではプログラム関連の記事を中心に書いてます。

HTTP通信のヘッダ情報を取得する

公式のリファレンスには記載されていないフィールドですが responseHeaders で取得出来ます。

C#のサンプル】

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test : MonoBehaviour {
	private string res;
	IEnumerator download () {
		string url = "http://www.unity3d.com/";
		WWW www = new WWW(url);
		yield return www;
		res = "";
		if (!string.IsNullOrEmpty(www.error) || string.IsNullOrEmpty(www.text)) {
			res = "error";
		} else {
  			foreach (KeyValuePair<string, string> kv in www.responseHeaders) {
  				res += kv.Key + " -> " + kv.Value + "\n";
  			}
		}
	}
	void OnGUI () {
		if (GUI.Button(new Rect(5, 5, 200, 20), "Test")) {
			StartCoroutine(download());
		}
		GUI.Label(new Rect(5, 30, Screen.width, 200), res);
	}
}