PHP版はこちら http://d.hatena.ne.jp/about_hiroppy/20120617/1339906476
例外処理(urlの存在確認)も大丈夫
今回は特に外見を変更せずに書きました。
Mainは上げません(というかvisual stdioが自動作成してくれるので)
つぶやきも実は簡単なのですがOAuth認証しないといけないので書きませんでした。
HttpStatusCode http://msdn.microsoft.com/ja-jp/library/system.net.httpstatuscode
ソース
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.IO; using System.Text; using System.Windows.Forms; using System.Web; using System.Net; using System.Xml; /* TL http://twitter.com/statuses/user_timeline.xml?screen_name={screen_name}&count={count}; */ /* private_information http://api.twitter.com/1/users/show.xml?screen_name={screen_name}; */ namespace twitter_API_timeline { public partial class Form1 : Form { public int transform(HttpStatusCode a) { int int_code = (int)a; return int_code; } public string information(string account,int t) { string res; string url; if(t == 0) url = string.Format("http://twitter.com/statuses/user_timeline.xml?screen_name={0}&count=200",account); else url = string.Format("http://api.twitter.com/1/users/show.xml?screen_name={0}", account); int code = transform(check_url(url)); // WebClient if (code < 400) { WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; byte[] data = client.DownloadData(url); res = Encoding.UTF8.GetString(data); //twitterはUTF-8 } else { res = "0"; } return res; } static public HttpStatusCode check_url(string url) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse res = null; HttpStatusCode statusCode; try { res = (HttpWebResponse)req.GetResponse(); statusCode = res.StatusCode; } catch (WebException ex) { res = (HttpWebResponse)ex.Response; if (res != null) { statusCode = res.StatusCode; } else { throw; // サーバ接続不可などの場合 } } finally { if (res != null) { res.Close(); } } return statusCode; } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string str = textBox1.Text; listBox1.Items.Clear(); //連続でlist登録の時に残らないように XmlDocument doc = new XmlDocument(); if (radioButton1.Checked) { string tl = information(str,0); if(tl == "0") listBox1.Items.Add("ユーザーが存在しないか鍵アカウントです。"); else { doc.LoadXml(tl); XmlNodeList nodes = doc.SelectNodes("/statuses/status/text"); foreach (XmlNode node in nodes) { listBox1.Items.Add(node.InnerText); //listBox1.Items.Add("\n"); } } } if (radioButton2.Checked) { string info = information(str, 1); if (info == "0") listBox1.Items.Add("そのユーザーは存在しません。"); else { doc.LoadXml(info); XmlNodeList nodes = doc.SelectNodes("/user"); foreach (XmlNode node in nodes) { foreach (XmlNode _node in node) { if (_node.Name == "id") listBox1.Items.Add("id\t\t"+_node.InnerText); if (_node.Name == "name") listBox1.Items.Add("name\t\t"+_node.InnerText); if (_node.Name == "screen_name") listBox1.Items.Add("screen_name\t"+_node.InnerText); if (_node.Name == "location") listBox1.Items.Add("location\t\t"+_node.InnerText); if (_node.Name == "description") { listBox1.Items.Add("description\t" + _node.InnerText); } if (_node.Name == "followers_count") listBox1.Items.Add("followers_count\t" + _node.InnerText); if (_node.Name == "friends_count") listBox1.Items.Add("friends_count\t" + _node.InnerText); if (_node.Name == "created_at") listBox1.Items.Add("created_at\t" + _node.InnerText); if (_node.Name == "favourites_count") listBox1.Items.Add("favourites_count\t" + _node.InnerText); if (_node.Name == "lang") listBox1.Items.Add("lang\t\t" + _node.InnerText); } } } } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { } } }
download -> https://skydrive.live.com/redir?resid=AD95B358FA50FB09!411