トラックバックの仕組み
using System.Text; using System.Net; using System.IO;//エンコード(Shift_JISで飛んでくる前提)
Encoding enc = Encoding.GetEncoding("Shift_JIS");//パラメータをURLエンコードしておく
string strTitle = HttpUtility.UrlEncode("トラックバックしてみた", enc);
string strExcerpt = HttpUtility.UrlEncode("記事の要約", enc);
string strUrl = HttpUtility.UrlEncode("トラックバックしたページのurl", enc);
string strBlogName = HttpUtility.UrlEncode("自分のサイトの名前", enc);//POSTで送信するデータを生成しておく
string strPostData;//この4項目を送るというのがルール
strPostData = "title=" + strTitle;
strPostData += "&excerpt=" + strExcerpt;
strPostData += "&url=" + strUrl;
strPostData += "&blog_name=" + strBlogName;//ストリームにのせる場合はバイト配列で
byte[] bytePostData = Encoding.ASCII.GetBytes(strPostData);//WebRequestの生成
WebRequest WebReq = WebRequest.Create("トラックバックURL");
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = bytePostData.Length;Stream WebReqStream = WebReq.GetRequestStream();
WebReqStream.Write(bytePostData, 0, bytePostData.Length);
WebReqStream.Close();//送るだけならWebRequestだけでいいが、今回はWebResponseで返り値を受け取る
WebResponse WebRes = WebReq.GetResponse();
Stream WebResStream = WebRes.GetResponseStream();
StreamReader sr = new StreamReader(WebResStream, enc);//返り値の出力
System.Diagnostics.Debug.Write(sr.ReadToEnd());sr.Close();
単にトラックバックするだけならPOSTでデータを送信すればいいのだが、
トラックバックは成功通知があるので、送りっぱなしというわけにはいかない。
コメントする