/// <summary> /// 自己紹介を変更します /// </summary> /// <param name="Description">自己紹介</param> internal void UpdateDescription(string Description) { var oah = new OAuthHelper(); try { var method = "POST"; var headerString = oah.BuildHeaderString(HttpUtility.UrlEncode(APIurl.updateProfileUrl), method, "description=" + Description, ""); var sendBytes = Encoding.UTF8.GetBytes("description=" + Description); var req = (HttpWebRequest)WebRequest.Create(APIurl.updateProfileUrl); req.Method = method; req.Headers.Add(HttpRequestHeader.Authorization, headerString); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = sendBytes.Length; req.ServicePoint.Expect100Continue = false; var reqStream = req.GetRequestStream(); reqStream.Write(sendBytes, 0, sendBytes.Length); reqStream.Close(); var res = (HttpWebResponse)req.GetResponse(); } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError) { if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedException(); } if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Forbidden) { throw new DuplicateTweetBodyException(); } } throw new TwitterServerNotWorkingWellException(); } catch { throw new TwitterServerNotWorkingWellException(); } }
/// <summary> /// 画像付きツイートを投稿します /// </summary> /// <param name="Body">本文</param> /// <param name="ImageFilePath">画像のパス</param> internal void PostTweetWithImage(string Body, string ImageFilePath) { var oah = new OAuthHelper(); if (Body.Length > 140) { throw new TooLongTweetBodyException(); } var method = "POST"; var headerString = oah.BuildHeaderString(HttpUtility.UrlEncode(APIurl.postTweetWithImageUrl), method, Body, ""); var randStr = DateTime.Now.Ticks.ToString("x"); var boundary = "--" + randStr; var openBytes = Encoding.UTF8.GetBytes( boundary + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Disposition: form-data; name=\"status\"\r\n" + "\r\n" + Body + "\r\n" + boundary + "\r\n" + "Content-Type: application/octet-stream\r\n" + "Content-Disposition: form-data; name=\"media[]\"; filename=\"" + Path.GetFileName(ImageFilePath) + "\"\r\n" + "\r\n" ); var closeBytes = Encoding.UTF8.GetBytes( "\r\n" + boundary + "--" ); try { var fs = new FileStream(ImageFilePath, FileMode.Open, FileAccess.Read); var req = (HttpWebRequest)WebRequest.Create(APIurl.postTweetWithImageUrl); req.Method = method; req.Headers.Add(HttpRequestHeader.Authorization, headerString); req.ContentType = "multipart/form-data; boundary=" + randStr; req.ContentLength = openBytes.Length + closeBytes.Length + fs.Length; req.Host = "api.twitter.com"; req.KeepAlive = true; req.ServicePoint.Expect100Continue = false; var reqStream = req.GetRequestStream(); reqStream.Write(openBytes, 0, openBytes.Length); var readData = new byte[0x1000]; var readSize = 0; while (true) { readSize = fs.Read(readData, 0, readData.Length); if (readSize == 0) { break; } reqStream.Write(readData, 0, readSize); } reqStream.Write(closeBytes, 0, closeBytes.Length); reqStream.Close(); fs.Close(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError) { if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedException(); } if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Forbidden) { throw new DuplicateTweetBodyException(); } } throw new TwitterServerNotWorkingWellException(); } catch { throw new TwitterServerNotWorkingWellException(); } }
/// <summary> /// ツイートを投稿します /// </summary> /// <param name="Body">本文</param> internal void PostTweetTextOnly(string Body, string In_reply_to_status_id = null) { var oah = new OAuthHelper(); if (Body.Length > 140) { throw new TooLongTweetBodyException(); } try { var method = "POST"; string headerString; byte[] sendBytes; if (In_reply_to_status_id != null) { headerString = oah.BuildHeaderString(HttpUtility.UrlEncode(APIurl.postTweetUrl), method, "in_reply_to_status_id=" + In_reply_to_status_id, "status=" + Uri.EscapeDataString(Body)); sendBytes = Encoding.UTF8.GetBytes("in_reply_to_status_id=" + In_reply_to_status_id + "&" + "status=" + Uri.EscapeDataString(Body)); } else { headerString = oah.BuildHeaderString(HttpUtility.UrlEncode(APIurl.postTweetUrl), method, "", "status=" + Uri.EscapeDataString(Body)); sendBytes = Encoding.UTF8.GetBytes("status=" + Uri.EscapeDataString(Body)); } var req = (HttpWebRequest)WebRequest.Create(APIurl.postTweetUrl); req.Method = method; req.Headers.Add(HttpRequestHeader.Authorization, headerString); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = sendBytes.Length; req.ServicePoint.Expect100Continue = false; var reqStream = req.GetRequestStream(); reqStream.Write(sendBytes, 0, sendBytes.Length); reqStream.Close(); var res = (HttpWebResponse)req.GetResponse(); } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError) { if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedException(); } if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Forbidden) { throw new DuplicateTweetBodyException(); } } throw new TwitterServerNotWorkingWellException(); } catch { throw new TwitterServerNotWorkingWellException(); } }
private static void GetUserStream(bool IsGetAllReplies) { var oah = new OAuthHelper(); while (isStartedUserStream) { WebResponse res = null; int i = 0; var method = "GET"; string headerString; string url; if (IsGetAllReplies) { url = APIurl.userStreamUrl + "?replies=all"; headerString = oah.BuildHeaderString(HttpUtility.UrlEncode(APIurl.userStreamUrl), method, "", "replies=all"); } else { url = APIurl.userStreamUrl; headerString = oah.BuildHeaderString(HttpUtility.UrlEncode(APIurl.userStreamUrl), method, "", ""); } do { var req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = method; req.Headers.Add(HttpRequestHeader.Authorization, headerString); req.Timeout = Timeout.Infinite; req.ServicePoint.Expect100Continue = false; try { res = req.GetResponse(); i = 0; } catch (WebException e) { if ((int)((HttpWebResponse)e.Response).StatusCode == 420) { var sleepTime = 5 * 1000 * Math.Pow(2, i); if (sleepTime > 300 * 100 * 1000) { break; } Thread.Sleep((int)sleepTime); i++; } else { var sleepTime = 5 * 1000 * Math.Pow(2, i); if (sleepTime > 320 * 1000) { break; } Thread.Sleep((int)sleepTime); i++; } } catch { var sleepTime = 250 * Math.Pow(2, i); if (sleepTime > 16 * 1000) { break; } Thread.Sleep((int)sleepTime); i++; } } while (res == null); var sr = new StreamReader(res.GetResponseStream()); while (isStartedUserStream == true) { try { string Text = sr.ReadLine(); if (Text != null && Text.Length > 0) { ParseJsonOfUserStream(Text); } else { Thread.Sleep(100); } } catch { break; } } try { res.Close(); } catch { } } if (isStartedUserStream) { throw new DeadOrDisconnectedUserStreamException(); } }
/// <summary> /// OAuthに必要なヘッダを生成します /// </summary> /// <param name="EncodedUrl">エンコード済みURL</param> /// <param name="Method">GET, POST, etc...</param> /// <param name="ExtString1">URLの直後に必要な追加シグネチャ</param> /// <param name="ExtString2">oauth_versionの直後に必要な追加シグネチャ</param> /// <returns>ヘッダ文字列</returns> public string BuildHeaderString(string EncodedUrl, string Method, string ExtString1, string ExtString2) { var oah = new OAuthHelper(); return(oah.BuildHeaderString(EncodedUrl, Method, ExtString1, ExtString2)); }