Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as C# by se ( 12 years ago )
public class PostWebClient
    {
        public delegate void WebClientCallback(Boolean error, string result);
        public WebClientCallback callback;
        public String postData = "";
        public CookieContainer cookies = null;

        public PostWebClient(WebClientCallback obj)
        {
            this.callback = new WebClientCallback(obj);
        }

        public void download(Uri address)
        {
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)System.Net.WebRequest.Create(address);
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13";
                request.Headers = new WebHeaderCollection();

                if (cookies != null)
                {
                    request.CookieContainer = cookies;
                }
                if (postData != "")
                {
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
                }
                else
                {
                    request.Method = "GET";
                    request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
                }
                
            }
            catch (Exception e)
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    callback(true, "Błąd #01"+e.ToString());
                });
            }
        }

        void RequestReady(IAsyncResult asyncResult)
        {
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;

            using (Stream stream = request.EndGetRequestStream(asyncResult))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write(postData);
                    writer.Flush();
                }
            }
            request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
        }

        void ResponseReady(IAsyncResult asyncResult)
        {
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.EndGetResponse(asyncResult);

                string result = string.Empty;
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    callback(false, result);
                });
            }
            catch (Exception e)
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    callback(true, "Błąd #02 - Brak połączenia z internetem"+e.ToString());
                });
            }
        }
    }

 

Revise this Paste

Your Name: Code Language: