private void TracingThread()
 {
     try {
         if (tracer.Trace(source, true))
         {
             success = true;
         }
     }
     catch (ThreadAbortException) {}
     catch (Exception ex) { TwinDll.ShowOutput(ex); }
     finally {
         Invoke(new MethodInvoker(OnFinally));
     }
 }
示例#2
0
        /// <summary>
        /// dataをサーバーに送信しレスポンスを得る
        /// </summary>
        /// <param name="data"></param>
        protected virtual PostResponse Posting(BoardInfo board, byte[] data, string uri, ref bool retried)
        {
            if (board == null)
            {
                throw new ArgumentNullException("board");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            HttpWebResponse    res    = null;
            PostResponseParser parser = null;

            // タイムアウト値
            const int timeout = 15000;             // 15秒

            // 再試行を行うかどうか
            bool is_retry = true;

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
                req.Method           = "POST";
                req.ContentType      = "application/x-www-form-urlencoded";
                req.ContentLength    = data.Length;
                req.Referer          = board.Url + "index.html";
                req.UserAgent        = UserAgent;
                req.Timeout          = timeout;
                req.ReadWriteTimeout = timeout;
                req.Proxy            = Proxy;
                //				req.Accept = "text/html, */*";
                //				req.Expect = null;
                //				req.AllowAutoRedirect = false;
                //				req.ProtocolVersion = HttpVersion.Version10;

                // NTwin 2011/05/31
                //req.CookieContainer = GetCookie(board);

                req.CookieContainer = CookieManager.gCookies;

#if DEBUG
                foreach (Cookie c in req.CookieContainer.GetCookies(req.RequestUri))
                {
                    Console.WriteLine("{0}={1}", c.Name, c.Value);
                }
#endif
                SetHttpWebRequest(req);

                Stream st = req.GetRequestStream();
                st.Write(data, 0, data.Length);
                st.Close();

                res = (HttpWebResponse)req.GetResponse();

                // レスポンスを解析するためのパーサを初期化。
                using (TextReader reader = new StreamReader(res.GetResponseStream(), Encoding))
                {
                    parser   = new PostResponseParser(reader.ReadToEnd());
                    response = parser.Response;

                    if (response == PostResponse.Cookie)
                    {
                        foreach (KeyValuePair <string, string> kv in parser.HiddenParams)
                        {
                            if (Regex.IsMatch(kv.Key, "subject|FROM|mail|MESSAGE|bbs|time|key") == false)
                            {
                                TwinDll.AditionalAgreementField = String.Format("&{0}={1}",
                                                                                kv.Key, kv.Value);

                                Console.WriteLine(TwinDll.AditionalAgreementField);
                                break;
                            }
                        }
                    }
                }

                if (res.StatusCode == HttpStatusCode.Found)
                {
                    response = PostResponse.Success;
                }

                /*
                 * board.CookieContainer = new CookieContainer();
                 *
                 * bool ponIsExist = false;
                 *
                 * foreach (Cookie c in req.CookieContainer.GetCookies(req.RequestUri))
                 * {
                 #if DEBUG
                 *      Console.WriteLine("{0}={1}", c.Name, c.Value);
                 #endif
                 *      if (c.Name == "PON")
                 *              ponIsExist = true;
                 *      board.CookieContainer.Add(c);
                 * }
                 *
                 * if (!ponIsExist && response == PostResponse.Cookie)
                 * {
                 *      board.CookieContainer.Add(res.Cookies);
                 * }*/

                // 投稿イベントを発生させる
                PostEventArgs e = new PostEventArgs(response, parser.Title,
                                                    parser.PlainText, null, parser.SambaCount);

                OnPosted(this, e);

                is_retry = e.Retry;
            }
            catch (Exception ex)
            {
#if DEBUG
                WebException webex = ex as WebException;
                if (webex != null)
                {
                    TwinDll.ShowOutput("Status " + webex.Status + ", " + webex.ToString());
                }
                else
                {
                    TwinDll.ShowOutput(ex);
                }
#endif
                // タイムアウトやそれ以外の例外が発生したら無条件でリトライを中止
                //is_retry = false;
                OnError(this, new PostErrorEventArgs(ex));
            }
            finally {
                if (res != null)
                {
                    res.Close();
                }

                // クッキー確認などでの再試行処理
                // ※既に再試行されていたら無限ループ防止のためfalseに設定
                if (retried)
                {
                    retried = false;
                }
                else
                {
                    retried = is_retry;
                }
            }

            return(response);
        }