Exemplo n.º 1
0
        private SecondStreetRawResponse postSecondStreetAPI(string url, Dictionary <string, string> param, string UserAgent)
        {
            SecondStreetRawResponse res = new SecondStreetRawResponse();

            try {
                string        text     = "";
                List <string> paramstr = new List <string>();
                int           num      = 0;
                foreach (KeyValuePair <string, string> p in param)
                {
                    string k = Uri.EscapeDataString(p.Key);
                    string v = Uri.EscapeDataString(p.Value);
                    if (num != 0)
                    {
                        text += "&";
                    }
                    text = text + (k + "=" + v);
                    num++;
                }
                byte[]         bytes = Encoding.ASCII.GetBytes(text);
                HttpWebRequest req   = (HttpWebRequest)WebRequest.Create(url);
                req.UserAgent = UserAgent;
                req.Method    = "POST";
                //リクエストヘッダを付加
                req.ContentType   = "application/x-www-form-urlencoded;";
                req.Accept        = "gzip, deflate";
                req.ContentLength = (long)bytes.Length;
                //クッキーコンテナの追加
                req.CookieContainer = this.cc;
                //タイムアウト設定
                req.Timeout = 500000;//でかくないとダメ
                //POST
                string content = "";
                var    task    = Task.Factory.StartNew(() => executePostRequest(ref req, bytes));
                //task.Wait(10000);
                task.Wait();
                if (task.IsCompleted)
                {
                    content = task.Result;
                }
                else
                {
                    throw new Exception("Timed out");
                }
                if (string.IsNullOrEmpty(content))
                {
                    throw new Exception("webrequest error");
                }
                res.error    = false;
                res.response = content;
                Log.Logger.Info("SecondStreetPOSTリクエスト成功");
                req.Abort();
                return(res);
            } catch (Exception e) {
                return(res);

                Log.Logger.Error("SecondStreetPOSTリクエスト失敗");
            }
        }
Exemplo n.º 2
0
        //FrilAPIをGETでたたく
        private SecondStreetRawResponse getSecondStreetAPI(string url, Dictionary <string, string> param, string UserAgent)
        {
            SecondStreetRawResponse res = new SecondStreetRawResponse();

            try
            {
                //url = Uri.EscapeUriString(url);//日本語などを%エンコードする
                //パラメータをURLに付加 ?param1=val1&param2=val2...
                url += "?";
                List <string> paramstr = new List <string>();
                foreach (KeyValuePair <string, string> p in param)
                {
                    string k = Uri.EscapeDataString(p.Key);
                    string v = Uri.EscapeDataString(p.Value);
                    paramstr.Add(k + "=" + v);
                }
                url += string.Join("&", paramstr);
                //HttpWebRequestの作成
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.CookieContainer = this.cc;
                req.UserAgent       = UserAgent;
                req.Method          = "GET";
                //結果取得
                string content = "";
                var    task    = Task.Factory.StartNew(() => executeGetRequest(req));
                task.Wait(10000);
                if (task.IsCompleted)
                {
                    content = task.Result;
                }
                else
                {
                    throw new Exception("Timed out");
                }
                if (string.IsNullOrEmpty(content))
                {
                    throw new Exception("webrequest error");
                }
                res.error    = false;
                res.response = content;
                Log.Logger.Info("SecondStreetGETリクエスト成功");
                return(res);
            }
            catch (Exception e)
            {
                Log.Logger.Error("SecondStreetGETリクエスト失敗");
                return(res);
            }
        }