public void InsertResult(string url, TProxyResult result) { try { TProxyResult res = Results[GetResultKey(url)]; Results.Remove(GetResultKey(url)); } catch { } finally { Results.Add(GetResultKey(url), result); } }
public void GETToSite(Control control, WebBrowser webBrowser, string url, Delegate packer) { TProxyResult result = new TProxyResult { Status = EComponentStatus.WORKING, Message = string.Empty, }; try { // Generate post objects Dictionary <string, object> postParameters = new Dictionary <string, object>(); Dictionary <string, object> postParametersLog = new Dictionary <string, object>(); if (packer != null) { control.Invoke(packer, postParameters); } // Parameters are written to log file in JSON string postParameteresJson = JsonConvert.SerializeObject(postParametersLog); //string postUrl = HttpUtility.UrlEncode(url + GetPHPFormatParams(postParameters)); string getUrl = url + GetPHPFormatParams(postParameters); if (!Properties.Settings.Default.IsDebug) { MyLogger.LogText("NEW GET TO: " + getUrl, "Proxy:: GETToSite"); } else { MyLogger.LogText("NEW DEBUG GET TO : " + getUrl, "Proxy:: GETToSite"); } try { if (Settings.Default.IsDebug) { MyLogger.LogText(getUrl); } MyLogger.LogText("BEGIN DATA GET", "Proxy:: GETToSite"); webBrowser.Navigate(getUrl, null, null, "User-Agent: " + GetUserAgent(EBrowser.Chrome)); MyLogger.LogText("END DATA GET", "Proxy:: GETToSite"); } catch (Exception ex) { result.Status = EComponentStatus.ERROR; result.Message = String.Format(Resources.Messages.SERVER_CONNECTION_ERROR, ex.Message); MyLogger.LogText("ERROR IN GET: " + result.Message, "Proxy:: GETToSite"); return; } result.Status = EComponentStatus.OK; } catch (Exception ex) { MyLogger.LogText("IN GETToSite" + ex.Message, "Proxy:: GETToSite"); result.Status = EComponentStatus.ERROR; result.Message = ex.Message; } finally { InsertResult(GetResultKey(url), result); } }
public void POSTToSite(string url, DlgPackPOSTData packer) { TProxyResult result = new TProxyResult { Status = EComponentStatus.WORKING, Message = string.Empty, ExceptionStatus = WebExceptionStatus.Success, }; try { // Generate post objects Dictionary <string, object> postParameters = new Dictionary <string, object>(); Dictionary <string, object> postParametersLog = new Dictionary <string, object>(); packer(postParameters, postParametersLog); // Parameters are written to log file in JSON string postParameteresJson = JsonConvert.SerializeObject(postParametersLog); string userAgent = Properties.Settings.Default.UserAgent; string postURL = url; try { if (Settings.Default.IsDebug) { MyLogger.LogText(postURL); MyLogger.LogText("POST Parameters"); foreach (KeyValuePair <string, object> item in postParameters) { MyLogger.LogText(item.Key + ": " + item.Value); } } MyLogger.LogText("BEGIN DATA POST TO " + postURL, "Proxy::POSTToSite"); result.Response = MultipartFormDataPost(postURL, userAgent, postParameters); MyLogger.LogText("END DATA POST", "Proxy::POSTToSite"); } catch (System.Net.WebException ex) { result.Status = EComponentStatus.ERROR; result.Message = Resources.Messages.SERVER_CONNECTION_ERROR; result.Response = ex.Response as HttpWebResponse; result.ExceptionStatus = ex.Status; MyLogger.LogText("ERROR IN POST: " + iQExceptionHandler.GetAllMessages(ex) + System.Environment.NewLine + "POST URL: " + postURL + System.Environment.NewLine + "POST RESPONSE: " + result.Response.ToString(), "Proxy::POSTToSite"); InsertResult(url, result); return; } if (result.Response.StatusCode == HttpStatusCode.OK) { // Process response StreamReader responseReader = new StreamReader(result.Response.GetResponseStream()); string fullResponse = responseReader.ReadToEnd(); result.Response.Close(); try { Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer(); Newtonsoft.Json.JsonReader r = new Newtonsoft.Json.JsonTextReader(new StringReader(fullResponse)); Newtonsoft.Json.Linq.JObject res = ser.Deserialize(r) as Newtonsoft.Json.Linq.JObject; StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, JToken> kvp in res) { switch (kvp.Key) { case "message": { result.Status = (kvp.Value.ToString() == "OK") ? EComponentStatus.OK : EComponentStatus.ERROR; sb.AppendLine((kvp.Value != null ? (kvp.Value.HasValues ? kvp.Value.First.ToString() : kvp.Value.ToString()) : "")); } break; default: sb.AppendLine(kvp.Value != null ? (kvp.Value.HasValues ? kvp.Value.First.ToString() : kvp.Value.ToString()) : ""); break; } } result.Message = sb.ToString(); } catch { MyLogger.LogText("JSON DESERIALIZE ERROR", "Proxy::POSTToSite"); MyLogger.LogText("FULL RESPONSE: " + fullResponse, "Proxy::POSTToSite"); result.Message = fullResponse; } } else { MyLogger.LogText("HTTP REQUEST STATUS ERROR.", "Proxy::POSTToSite"); MyLogger.LogText("HTTP URL: " + url, "Proxy::POSTToSite"); } } catch (Exception ex) { MyLogger.LogText("IN POSTToSite: " + ex.Message, "Proxy::POSTToSite"); result.Status = EComponentStatus.ERROR; result.Message = ex.Message; } finally { InsertResult(url, result); } }
public void POSTToSite(Control control, string url, Delegate packer) { TProxyResult result = new TProxyResult { Status = EComponentStatus.WORKING, Message = string.Empty, Response = null, ExceptionStatus = WebExceptionStatus.Success, }; try { // Generate post objects Dictionary <string, object> postParameters = new Dictionary <string, object>(); Dictionary <string, object> postParametersLog = new Dictionary <string, object>(); control.Invoke(packer, new object[2] { postParameters, postParametersLog }); string postURL = url; string userAgent = Properties.Settings.Default.UserAgent; // Parameters are written to log file in JSON string postParameteresJson = JsonConvert.SerializeObject(postParametersLog); if (!Properties.Settings.Default.IsDebug) { MyLogger.LogText("NEW POST. " + postParameteresJson, "Proxy::POSTToSite"); } else { MyLogger.LogText("NEW DEBUG POST. " + postParameteresJson, "Proxy::POSTToSite"); } try { if (Settings.Default.IsDebug) { MyLogger.LogText(postURL); foreach (KeyValuePair <string, object> item in postParameters) { MyLogger.LogText(item.Key + ": " + item.Value); } } MyLogger.LogText("BEGIN DATA POST TO " + postURL, "Proxy::POSTToSite"); result.Response = MultipartFormDataPost(postURL, userAgent, postParameters); MyLogger.LogText("END DATA POST", "Proxy::POSTToSite"); } catch (System.Net.WebException ex) { result.Status = EComponentStatus.ERROR; result.Message = String.Format(Resources.Messages.SERVER_CONNECTION_ERROR, ex.Message); result.Response = ex.Response as HttpWebResponse; result.ExceptionStatus = ex.Status; MyLogger.LogText("ERROR IN POST: " + ex.Message + System.Environment.NewLine + "POST URL: " + postURL , "Proxy::POSTToSite"); InsertResult(url, result); return; } if (result.Response.StatusCode == HttpStatusCode.OK) { MyLogger.LogText("HTTP STATUS OK: " + result.Message, "Proxy::POSTToSite"); result.Status = EComponentStatus.ERROR; // Process response StreamReader responseReader = new StreamReader(result.Response.GetResponseStream()); string fullResponse = responseReader.ReadToEnd(); result.Response.Close(); MyLogger.LogText("FULL RESPONSE: " + fullResponse, "Proxy::POSTToSite"); try { MyLogger.LogText("JSON DESERIALIZE BEGIN", "Proxy::POSTToSite"); Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer(); Newtonsoft.Json.JsonReader r = new Newtonsoft.Json.JsonTextReader(new StringReader(fullResponse)); Newtonsoft.Json.Linq.JObject res = ser.Deserialize(r) as Newtonsoft.Json.Linq.JObject; StringBuilder sb = new StringBuilder(); sb.AppendLine("Server response"); foreach (KeyValuePair <string, JToken> kvp in res) { sb.AppendLine(string.Format("{0} - {1}", kvp.Key, kvp.Value != null ? (kvp.Value.HasValues ? kvp.Value.First.ToString() : kvp.Value.ToString()) : "")); } result.Message = sb.ToString(); MyLogger.LogText("JSON DESERIALIZE END. RESULT: " + result.Message, "Proxy::POSTToSite"); InsertResult(url, result); return; } catch { MyLogger.LogText("JSON DESERIALIZE ERROR", "Proxy::POSTToSite"); result.Message = fullResponse; InsertResult(url, result); return; } } else { result.Status = EComponentStatus.OK; MyLogger.LogText("NO RESPONSE ERROR. USER REGISTERED. REDIRECTION.", "Proxy::POSTToSite"); InsertResult(url, result); return; } } catch (Exception ex) { MyLogger.LogText("IN POSTToSite: " + ex.Message, "Proxy::POSTToSite"); result.Status = EComponentStatus.ERROR; result.Message = ex.Message; InsertResult(url, result); } }