public static AddressResult LookupAddress(string line1, string line2, string city, string st, string zip) { string url = ConfigurationManager.AppSettings["amiurl"]; string password = ConfigurationManager.AppSettings["amipassword"]; if (!url.HasValue() || !password.HasValue()) { return new AddressResult { Line1 = line1, Line2 = line2, City = city, State = st, Zip = zip } } ; var wc = new WebClientWithTimeout(); var coll = new NameValueCollection { { "line1", line1 }, { "line2", line2 }, { "csz", Util.FormatCSZ(city, st, zip) }, { "passcode", password } }; try { var resp = wc.UploadValues(url, "POST", coll); var s = Encoding.UTF8.GetString(resp); s = s.Replace("�", "1/2"); var serializer = new XmlSerializer(typeof(AddressResult)); var reader = new StringReader(s); var ret = (AddressResult)serializer.Deserialize(reader); if (ret.found == null) { ret.found = false; } if (ret.found == true && ret.error.HasValue()) { ret.found = false; } if (ret.error == "unauthorized") { ret.Line1 = "error"; } if (ret.ExpireDays < 0 || ret.address.Trim() == ",") { ret.Line1 = "AMS Database expired"; ret.found = false; } return(ret); } catch (Exception) { return(new AddressResult { Line1 = "error" }); } }
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: EdcbSchUploader <user> <pass> [epgurl] [slot] [upload_url]"); Environment.Exit(2); } // サービス名としょぼかる放送局名の対応ファイル"SyoboiCh.txt"があれば読み込む var chMap = new Dictionary <string, string>(); try { string[] chList = File.ReadAllText( Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "SyoboiCh.txt"), Encoding.GetEncoding(932) ).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string ch in chList) { string[] chSplit = ch.Split(new char[] { '\t' }); if (chSplit.Length >= 2 && chSplit[0].StartsWith(";") == false) { chMap[chSplit[0]] = chSplit[1]; } } } catch { } // EpgTimerSrvから予約情報を取得 var cmd = new CtrlCmdUtil(); var rl = new List <ReserveData>(); var tl = new List <TunerReserveInfo>(); if (cmd.SendEnumReserve(ref rl) != ErrCode.CMD_SUCCESS || cmd.SendEnumTunerReserve(ref tl) != ErrCode.CMD_SUCCESS) { Console.WriteLine("Failed to communicate with EpgTimerSrv."); Environment.Exit(1); } string data = ""; int dataCount = 0; foreach (ReserveData r in rl) { string tuner = null; foreach (TunerReserveInfo tr in tl) { if (tr.reserveList.Contains(r.ReserveID)) { // T+{BonDriver番号(上2桁)}+{チューナ番号(下2桁)},チューナ不足はT9999 tuner = "T" + ("" + (10000 + Math.Min(tr.tunerID / 65536, 99) * 100 + Math.Min(tr.tunerID % 65536, 99))).Substring(1); break; } } if (tuner != null && r.RecSetting.RecMode <= 3 && r.RecSetting.Priority >= UPLOAD_REC_PRIORITY) { // "最大200行" if (dataCount++ >= 200) { break; } else { // "開始時間 終了時間 デバイス名 番組名 放送局名 サブタイトル オフセット XID" string title = r.Title.Replace("\t", "").Replace("\r", "").Replace("\n", ""); data += "" + (r.StartTime.ToUniversalTime() - (new DateTime(1970, 1, 1))).TotalSeconds + "\t" + (r.StartTime.ToUniversalTime().AddSeconds(r.DurationSecond) - (new DateTime(1970, 1, 1))).TotalSeconds + "\t" + tuner + "\t" + (title.Length > UPLOAD_TITLE_MAXLEN ? title.Substring(0, UPLOAD_TITLE_MAXLEN - 1) + "…" : title) + "\t" + (chMap.ContainsKey(r.StationName) ? chMap[r.StationName] : r.StationName) + "\t\t0\t" + r.ReserveID + "\n"; } } } // 以前の内容と同じなら送信しない string lastPath = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "last"); try { if (File.ReadAllText(lastPath) == data.Replace("\n", "\r\n")) { Console.WriteLine("Not modified, nothing to do."); Environment.Exit(0); } } catch { } // しょぼかるのPOSTは"Expect: 100-Continue"に対応してないっぽい ServicePointManager.Expect100Continue = false; // 予約情報をサーバに送信 using (var wc = new WebClientWithTimeout()) { // WebClientはデフォルトでIEのプロキシ設定に従う(wc.Proxy = WebRequest.DefaultWebProxy) // 認証プロキシなど必要であればwc.Proxyをここで設定すること wc.Timeout = TIMEOUT; wc.Encoding = Encoding.UTF8; wc.Credentials = new NetworkCredential(Uri.EscapeDataString(args[0]), Uri.EscapeDataString(args[1])); wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; wc.Headers[HttpRequestHeader.UserAgent] = USER_AGENT; var body = new System.Collections.Specialized.NameValueCollection(); body["slot"] = args.Length < 4 ? "0" : args[3]; body["devcolors"] = DEV_COLORS; body["epgurl"] = args.Length < 3 ? "" : args[2]; body["data"] = data; try { wc.UploadValues(args.Length < 5 ? UPLOAD_URL : args[4], body); File.WriteAllText(lastPath, data.Replace("\n", "\r\n"), Encoding.UTF8); } catch (Exception ex) { Console.WriteLine(ex.GetType().ToString() + " : " + ex.Message); Environment.Exit(1); } } Console.WriteLine("Done."); Environment.Exit(0); }
private byte[] Post(string url, Dictionary <string, object> values) { byte[] response = null; using (WebClient client = new WebClientWithTimeout(this.Timeout, this.KeepAlive)) { if (XHttpRequestOverride) { client.Headers.Add("X-HTTP-Method-Override:" + Method.ToString()); client.Headers.Add("X-HTTP-Method:" + Method.ToString()); client.Headers.Add("X-METHOD-OVERRIDE:" + Method.ToString()); } if (!string.IsNullOrWhiteSpace(Header)) { var headers = Header.Split('\n'); foreach (var header in headers) { if (!string.IsNullOrWhiteSpace(header)) { client.Headers.Add(header); } } } NameValueCollection formData = null; switch (ContentType) { case HttpRequestContentType.JSON: client.Headers.Add("Content-Type: application/json"); string json; if (values.Count != 1 || IsPrimitive(values.First().Value)) { json = Newtonsoft.Json.JsonConvert.SerializeObject(values); } else { json = Newtonsoft.Json.JsonConvert.SerializeObject(values.First()); } response = client.UploadData(url, method: XHttpRequestOverride ? "POST" : Method.ToString(), data: Encoding.UTF8.GetBytes(json)); break; case HttpRequestContentType.FormData: formData = new NameValueCollection(); foreach (var value in values) { formData.Add(value.Key, (value.Value ?? string.Empty).ToString()); } //upload values já seta content-type para application/x-www-form-urlencoded response = client.UploadValues(url, method: XHttpRequestOverride ? "POST" : Method.ToString(), data: formData); break; case HttpRequestContentType.XML: client.Headers.Add("Content-Type: application/xml"); if (values.Count != 1 || !IsPrimitive(values.First().Value)) { throw new ArgumentException(); } XmlSerializer ser = new XmlSerializer(values.First().Value.GetType()); System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter writer = new System.IO.StringWriter(sb); ser.Serialize(writer, values.First().Value); response = client.UploadData(url, XHttpRequestOverride ? "POST" : Method.ToString(), Encoding.UTF8.GetBytes(values.First().Value as string)); break; case HttpRequestContentType.Bytes: client.Headers.Add("Content-Type: application/octet-stream"); if (values.Count != 1 || !(values.First().Value is byte[])) { throw new ArgumentException(); } response = client.UploadData(url, XHttpRequestOverride ? "POST" : Method.ToString(), values.First().Value as byte[]); break; case HttpRequestContentType.String: client.Headers.Add("Content-Type: text/plain;charset=utf-8"); if (values.Count != 1 || !(values.First().Value is byte[])) { throw new ArgumentException(); } response = client.UploadData(url, XHttpRequestOverride ? "POST" : Method.ToString(), Encoding.UTF8.GetBytes(values.First().Value as string)); break; default: goto case HttpRequestContentType.JSON; } } return(response); }