private void UploadAsync2(AsyncArgs args) { PostDataUpload dl = new PostDataUpload(); this.PrepareDownloader(dl, args); if (dl.Settings.PostStringData != string.Empty) { dl.AsyncDownloadCompleted += this.dl_DownloadAsyncCompleted; dl.DownloadAsync(args.UserArgs); } }
public void UploadAsync(WebFormDownloadSettings settings, object userArgs) { AsyncArgs args = new AsyncArgs(userArgs) { Settings = settings }; if (settings.Account.Crumb == string.Empty) { Html2XmlDownload html = new Html2XmlDownload(); html.Settings.Account = settings.Account; html.Settings.Url = settings.Url; html.AsyncDownloadCompleted += this.html_DownloadAsyncCompleted; html.DownloadAsync(args); } else { this.UploadAsync2(args); } }
public Response<XDocument> Upload(WebFormDownloadSettings settings) { AsyncArgs args = new AsyncArgs(null) { Settings = settings }; if (settings.Account.Crumb == string.Empty) { Html2XmlDownload html = new Html2XmlDownload(); html.Settings.Account = settings.Account; html.Settings.Url = settings.Url; Response<XDocument> resp = html.Download(); this.ConvertHtml(resp.Result, args); } PostDataUpload dl = new PostDataUpload(); this.PrepareDownloader(dl, args); if (dl.Settings.PostStringData != string.Empty) { DefaultResponse<System.IO.Stream> resp = (DefaultResponse<System.IO.Stream>)dl.Download(); return resp.CreateNew(MyHelper.ParseXmlDocument(resp.Result)); } else { return null; } }
public IAsyncHandle QueryEx <T>(HttpRequest request, EventHandler <RestClientEventArgs <T> > callback) { string acceptType = ContentType; if (request == null) { throw new ArgumentNullException("request"); } DisplayLoading(true); string url = request.RequestUri.ToString(); url = SetAcceptType(url, acceptType); url = SetLanguageType(url, Thread.CurrentThread.CurrentUICulture.Name); url = SetIdentityAndTimeZone(url); request = new HttpRequest(url) { Method = Operating.GET }; var args = new AsyncArgs(request, null, typeof(T), callback, new RestClientEventArgs <T>(this.Page), acceptType); var asyncResult = request.BeginGetResponse(OnGetResponse, args); return(new RestClientAsyncHandle(request, asyncResult)); }
private void ConvertHtml(XDocument doc, AsyncArgs args) { string fap = string.Empty; if (args.Settings.FormActionPattern != string.Empty) { fap = "//form[@" + args.Settings.FormActionPattern + "]"; } else { fap = "//form[@action=\"" + args.Settings.RefererUrlPart + "\"]"; } XElement formNode = new XPath(fap, true).GetElement(doc); if (formNode != null) { XElement[] inputNodes = XPath.GetElements("//input", formNode); foreach (XElement inp in inputNodes) { XAttribute nameAtt = inp.Attribute(XName.Get("name")); if (nameAtt != null) { string value = string.Empty; XAttribute valueAtt = inp.Attribute(XName.Get("value")); if (valueAtt != null && valueAtt.Value != string.Empty) { value = valueAtt.Value; } if (nameAtt.Value.Contains("challenge")) { args.IsLoginChallenge = true; } if (nameAtt.Value.Contains("crumb")) { args.Settings.Account.SetCrumb(valueAtt.Value); } else { if (args.Settings.SearchForWebForms != null) { foreach (string name in args.Settings.SearchForWebForms) { if (name == nameAtt.Value) { if (args.Settings.AdditionalWebForms != null && args.Settings.AdditionalWebForms.Count > 0) { for (int i = 0; i < args.Settings.AdditionalWebForms.Count; i++) { if (args.Settings.AdditionalWebForms[i].Key == nameAtt.Value) { args.Settings.AdditionalWebForms[i] = new KeyValuePair<string, string>(nameAtt.Value, valueAtt.Value); break; } } } break; } } } } } } } }
private void PrepareDownloader(PostDataUpload dl, AsyncArgs args) { if (args.Settings.Account.Crumb != string.Empty || args.IsLoginChallenge) { if (args.Settings.DownloadResponse) dl.Settings.DownloadResponse = true; bool setCrumb = true; if (args.Settings.AdditionalWebForms.Count > 0 && !args.IsLoginChallenge) { for (int i = 0; i < args.Settings.AdditionalWebForms.Count; i++) { if (args.Settings.AdditionalWebForms[i].Key == ".yficrumb") { args.Settings.AdditionalWebForms[i] = new KeyValuePair<string, string>(".yficrumb", args.Settings.Account.Crumb); setCrumb = false; break; } } } if (args.IsLoginChallenge) setCrumb = false; if (setCrumb) args.Settings.AdditionalWebForms.Insert(0, new KeyValuePair<string, string>(".yficrumb", args.Settings.Account.Crumb)); if (args.Settings.RefererUrlPart.StartsWith("http")) { args.Settings.Url = args.Settings.RefererUrlPart; } else { args.Settings.Url = "http://" + new Uri(args.Settings.Url).Host + args.Settings.RefererUrlPart; } dl.Settings.UrlString = args.Settings.Url; dl.Settings.Account = args.Settings.Account; StringBuilder postData = new StringBuilder(); bool isFirst = true; foreach (var kvp in args.Settings.AdditionalWebForms) { string data = Uri.EscapeDataString(kvp.Key) + "=" + Uri.EscapeDataString(kvp.Value); if (isFirst) { isFirst = false; } else { data = "&" + data; } postData.Append(data); } dl.Settings.PostStringData = postData.ToString(); } }
private void OnGetResponse <T>(IAsyncResult result) { AsyncArgs <T> args = (AsyncArgs <T>)result.AsyncState; HttpWebResponse response = null; object data = null; RestServiceError error = null; try { response = args.Request.EndGetResponse(result) as HttpWebResponse; ISerializer serializer = SerializerFactory.GetSerializer((args.Request.Accept == null || args.Request.Accept.Length == 0) ? ContentType : args.Request.Accept); string responseTxt; using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseTxt = readStream.ReadToEnd(); } if (!ErrorHandle(out error, responseTxt, serializer)) { if (args.IsDynamic) { data = DynamicXml.Parse(responseTxt); } else { using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(responseTxt))) { data = (T)serializer.Deserialize(stream, typeof(T)); } } } } catch (WebException ex) { HttpWebResponse e = ex.Response as HttpWebResponse; error = new RestServiceError(); error.StatusCode = e.StatusCode.GetHashCode(); error.StatusDescription = e.StatusDescription; error.Faults = new List <Error>(); error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); e.Close(); ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), new object[0]); } catch (Exception ex) { error = new RestServiceError(); error.StatusCode = 500; error.Faults = new List <Error>(); error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), new object[0]); } finally { if (response != null) { response.Close(); } } if (error == null) { Action <T> handler = args.SucceedHandler; if (handler != null) { handler((T)data); } } else { Action <RestServiceError> eHandler = args.ErrorHandler; if (eHandler != null) { eHandler(error); } } }
private void ConvertHtml(XDocument doc, AsyncArgs args) { string fap = string.Empty; if (args.Settings.FormActionPattern != string.Empty) { fap = "//form[@" + args.Settings.FormActionPattern + "]"; } else { fap = "//form[@action=\"" + args.Settings.RefererUrlPart + "\"]"; } XElement formNode = new XPath(fap, true).GetElement(doc); if (formNode != null) { XElement[] inputNodes = XPath.GetElements("//input", formNode); foreach (XElement inp in inputNodes) { XAttribute nameAtt = inp.Attribute(XName.Get("name")); if (nameAtt != null) { string value = string.Empty; XAttribute valueAtt = inp.Attribute(XName.Get("value")); if (valueAtt != null && valueAtt.Value != string.Empty) { value = valueAtt.Value; } if (nameAtt.Value.Contains("challenge")) { args.IsLoginChallenge = true; } if (nameAtt.Value.Contains("crumb")) { args.Settings.Account.SetCrumb(valueAtt.Value); } else { if (args.Settings.SearchForWebForms != null) { foreach (string name in args.Settings.SearchForWebForms) { if (name == nameAtt.Value) { if (args.Settings.AdditionalWebForms != null && args.Settings.AdditionalWebForms.Count > 0) { for (int i = 0; i < args.Settings.AdditionalWebForms.Count; i++) { if (args.Settings.AdditionalWebForms[i].Key == nameAtt.Value) { args.Settings.AdditionalWebForms[i] = new KeyValuePair <string, string>(nameAtt.Value, valueAtt.Value); break; } } } break; } } } } } } } }
private void OnGetResponse(IAsyncResult result) { AsyncArgs args = result.AsyncState as AsyncArgs; HttpWebResponse response = null; object data = null; RestServiceError error = null; try { response = args.Request.EndGetResponse(result) as HttpWebResponse; ISerializer serializer = SerializerFactory.GetSerializer((args.Request.Accept == null || args.Request.Accept.Length == 0) ? args.UserState.ToString() : args.Request.Accept); if (serializer != null) { if (!ErrorHandle(out error, response, serializer)) { data = serializer.Deserialize(response.GetResponseStream(), args.DataType); } } } catch (WebException ex) { //HttpWebResponse e = ex.Response as HttpWebResponse; //error = new RestServiceError(); //error.StatusCode = e.StatusCode.GetHashCode(); //error.StatusDescription = e.StatusDescription; //error.Faults = new System.Collections.ObjectModel.ObservableCollection<Error>(); //error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); //e.Close(); HttpWebResponse e = ex.Response as HttpWebResponse; if (e != null) { error = new RestServiceError(); error.StatusCode = e.StatusCode.GetHashCode(); error.StatusDescription = e.StatusDescription; error.Faults = new System.Collections.ObjectModel.ObservableCollection <Error>(); error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); e.Close(); } else if (ex.Response != null) { error = new RestServiceError(); error.StatusCode = -1; error.StatusDescription = "未知错误,返回的WebException.Response类型为:" + ex.Response.GetType(); error.Faults = new System.Collections.ObjectModel.ObservableCollection <Error>(); error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); } else { error = new RestServiceError(); error.StatusCode = -2; error.StatusDescription = "未知错误,返回的WebException.Response为null。"; error.Faults = new System.Collections.ObjectModel.ObservableCollection <Error>(); error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); } ComponentFactory.Logger.LogError(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), null); } catch (Exception ex) { error = new RestServiceError(); error.StatusCode = 500; error.Faults = new System.Collections.ObjectModel.ObservableCollection <Error>(); error.Faults.Add(new Error() { ErrorCode = "00000", ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString()) }); ComponentFactory.Logger.LogError(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), null); } finally { DisplayLoading(false); if (response != null) { response.Close(); } } CallBackHandle(data, error, args); }
public override void OnSocketError(AsyncArgs args, SocketError err) { Console.WriteLine("Socket error: " + err.ToString()); Close(); }
//AsyncArgs is SocketAsyncEventArgs public override void OnSocketError(AsyncArgs args, SocketError err) { Console.WriteLine("[CLIENT]SocketError " + err); }