Inheritance: System.Net.WebClient
Beispiel #1
0
        /// <summary>
        /// Communicates to and from a Server
        /// </summary>
        /// <param name="address">The URI to use</param>
        /// <param name="data">The Data to send</param>
        /// <returns>The response from Server</returns>
        public static string Transmit(string address, string data)
        {
            try
            {
                ServicePointManager.Expect100Continue = false;
                ExtendedWebClient client = new ExtendedWebClient();
                client.Timeout = 30000;
                client.Encoding = Encoding.UTF8;
                client.Headers.Add("user-agent", AppSettings.UserAgent);
                if (string.IsNullOrEmpty(data))
                    return client.DownloadString(address);
                else
                    return client.UploadString(address, data);
            }
            catch (WebException e)
            {
                string ret = null;

                // something bad happened
                var response = e.Response as HttpWebResponse;
                try
                {
                    using (var stream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            ret = reader.ReadToEnd();
                        }
                    }
                }
                catch { }
                return ret;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Communicates to and from a Server
        /// </summary>
        /// <param name="address">The URI to use</param>
        /// <param name="data">The Data to send</param>
        /// <returns>The response from Server</returns>
        public static string Transmit(string address, string data, bool logResponse = true)
        {
            if (OnDataSend != null)
                OnDataSend(address, data);

            try
            {
                ServicePointManager.Expect100Continue = false;
                ExtendedWebClient client = new ExtendedWebClient();
                client.Timeout = 90000;
                client.Encoding = Encoding.UTF8;
                client.Headers.Add("user-agent", AppSettings.UserAgent);

                string response = string.Empty;

                if (string.IsNullOrEmpty(data))
                    response = client.DownloadString(address);
                else
                    response = client.UploadString(address, data);

                if (logResponse && OnDataReceived != null)
                    OnDataReceived(response);

                return response;
            }
            catch (WebException we)
            {
                string ret = null;

                // something bad happened
                var response = we.Response as HttpWebResponse;
                try
                {
                    using (var stream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            ret = reader.ReadToEnd();

                            if (OnDataErrorReceived != null)
                                OnDataErrorReceived(ret);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (OnDataErrorReceived != null)
                        OnDataErrorReceived(e.Message);
                }
                return ret;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Communicates to and from a Server
        /// </summary>
        /// <param name="address">The URI to use</param>
        /// <param name="data">The Data to send</param>
        /// <param name="logResponse">Shall we log the response?</param>
        /// <returns>The response from Server</returns>
        public static string Transmit(string address, string data, bool logResponse = true)
        {
            if (OnDataSend != null)
            {
                OnDataSend(address, data);
            }

            try
            {
                ServicePointManager.Expect100Continue = false;
                var client = new ExtendedWebClient {
                    Timeout = 120000, Encoding = Encoding.UTF8
                };
                client.Headers.Add("user-agent", AppSettings.UserAgent);

                var response = string.Empty;

                if (string.IsNullOrEmpty(data))
                {
                    response = client.DownloadString(address);
                }
                else
                {
                    response = client.UploadString(address, data);
                }

                if (logResponse && OnDataReceived != null)
                {
                    OnDataReceived(response);
                }

                return(response);
            }
            catch (WebException we)
            {
                string ret = null;

                // something bad happened
                var response = we.Response as HttpWebResponse;
                try
                {
                    using (var stream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            ret = reader.ReadToEnd();

                            if (OnDataErrorReceived != null)
                            {
                                OnDataErrorReceived(ret);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (OnDataErrorReceived != null)
                    {
                        OnDataErrorReceived(e.Message);
                    }
                }
                return(ret);
            }
        }