1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C#: Anyone have a WebRequests class?

Discussion in 'C, C++, C#' started by kytro360, Dec 14, 2013.

  1. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    703
    Likes Received:
    732
    Hey everyone!

    I was wondering if anyone here has a Webrequests class they can share?

    I have been using one for some time now which lets me do the usual POST and GET request, but am trying to find one which also has code to do multipart requests.

    Any classes you may use to help would be greatly appreciated!

    P.S:

    I've pasted in the FULL class I use currently. I've also highlighted the parts of the code where I tried implementing multipart requests (got some code online and tried squeezing it in) but couldn't figure it all out.

    If anyone can help me figure it out it'd be greatly appreciated:


    HTTP WEBREQUESTS CLASS-
    Code:
    using System;
    using System.Net;
    using System.IO;
    using System.Text;
    
    public class HttpInterface
    {
        private CookieContainer local_cookies = new CookieContainer();
        private string local_user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11";
        private bool local_allow_auto_redirect = false;
        private bool local_keep_cookies = true;
        private WebProxy local_proxy = null;
        private int local_timeout = 100000;
        private bool local_ajax = false;
        private string local_error = string.Empty;
    
        #region Settings
        public string user_agent
        {
            get
            {
                return this.local_user_agent;
            }
            set
            {
                this.local_user_agent = value;
            }
        }
    
        public string proxy
        {
            get
            {
                return this.local_proxy.Address.Host + ":" + this.local_proxy.Address.Port;
            }
            set
            {
                if (valid_proxy(value))
                {
                    string[] splitted = value.Split(':');
                    this.local_proxy = new WebProxy(splitted[0], Convert.ToInt32(splitted[1]));
                }
            }
        }
    
        public int timeout
        {
            get
            {
                return this.local_timeout;
            }
            set
            {
                this.local_timeout = value;
            }
        }
    
        public bool allow_auto_redirect
        {
            get
            {
                return this.local_allow_auto_redirect;
            }
            set
            {
                this.local_allow_auto_redirect = value;
            }
        }
    
        public bool keep_cookies
        {
            get
            {
                return this.local_keep_cookies;
            }
            set
            {
                this.local_keep_cookies = value;
            }
        }
    
        public bool ajax
        {
            get
            {
                return this.local_ajax;
            }
            set
            {
                this.local_ajax = value;
            }
        }
        #endregion
    
        #region Main
        public string request(string method, string url, string data, string referer)
        {
            this.local_error = string.Empty;
            if (valid_url(url))
            {
                HttpWebResponse response = null;
                StreamReader stream_reader = null;
                try
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.Method = (method = method.ToUpper());
                    request.Referer = referer;
                    request.AllowAutoRedirect = this.local_allow_auto_redirect;
                    request.CookieContainer = this.local_cookies;
                    request.UserAgent = this.local_user_agent;
                    request.Timeout = this.local_timeout;
                    request.Proxy = this.local_proxy;
                    if (this.local_ajax)
                    {
                        request.Headers.Add("X-Requested-With", "XMLHttpRequest");
                    }
                    if (method.Equals("POST"))
                    {
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.ContentLength = data.Length;
                        byte[] bytes = Encoding.ASCII.GetBytes(data);
                        if (bytes.Length == data.Length)
                        {
                            Stream data_stream = request.GetRequestStream();
                            data_stream.Write(bytes, 0, bytes.Length);
                            data_stream.Close();
                        }
                    }
                    response = (HttpWebResponse)request.GetResponse();
                    foreach (Cookie cookie in response.Cookies)
                    {
                        this.local_cookies.Add(cookie);
                    }
                    stream_reader = new StreamReader(response.GetResponseStream());
                    return stream_reader.ReadToEnd();
                }
                catch (Exception ex)
                {
                    this.local_error = ex.StackTrace;
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                    if (stream_reader != null)
                    {
                        stream_reader.Close();
                    }
                }
            }
            else
            {
                this.local_error = "The URL '" + url + "' is invalid.";
            }
            return string.Empty;
        }
    
        public string get(string url)
        {
            return this.request("GET", url, string.Empty, string.Empty);
        }
    
        public string get(string url, string referal)
        {
            return this.request("GET", url, string.Empty, referal);
        }
    
        public string post(string url, string data)
        {
            return this.request("POST", url, data, string.Empty);
        }
    
        public string post(string url, string data, string referal)
        {
            return this.request("POST", url, data, referal);
        }
        #endregion
    
        #region Validate
        public bool valid_url ( string url )
        {
            Uri create;
            return Uri.TryCreate ( url, UriKind.RelativeOrAbsolute, out create );
        }
    
        public bool valid_proxy ( string proxy )
        {
            string [ ] splitted = proxy.Split ( ':' );
            if ( splitted.Length == 2 && valid_url ( splitted [ 0 ] ) )
            {
                try
                {
                    new WebProxy ( splitted [ 0 ], Convert.ToInt32 ( splitted [ 1 ] ) );
                    return true;
                }
                catch { }
            }
            return false;
        }
        #endregion
    }
    


    The multipart request code I tried putting in the class but it didn't work:

    Code:
      public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
        {
            string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
            string contentType = "multipart/form-data; boundary=" + formDataBoundary;
    
            byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
    
            return PostForm(postUrl, userAgent, contentType, formData);
        }
        public string HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData)
        {
            HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
    
            if (request == null)
            {
                throw new NullReferenceException("request is not a http request");
            }
    
            // Set up the request properties.
            request.Method = "POST";
            request.ContentType = contentType;
            request.UserAgent = userAgent;
            request.CookieContainer = this.local_cookies;
            request.ContentLength = formData.Length;
    
            // You could add authentication here as well if needed:
            // request.PreAuthenticate = true;
            // request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
            // request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("username" + ":" + "password")));
    
            // Send the form data to the request.
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(formData, 0, formData.Length);
                requestStream.Close();
            }
    
            return request.GetResponse() as HttpWebResponse;
        }
    
        private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
        {
            Stream formDataStream = new System.IO.MemoryStream();
            bool needsCLRF = false;
    
            foreach (var param in postParameters)
            {
                // Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
                // Skip it on the first parameter, add it to subsequent parameters.
                if (needsCLRF)
                    formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
    
                needsCLRF = true;
    
                if (param.Value is FileParameter)
                {
                    FileParameter fileToUpload = (FileParameter)param.Value;
    
                    // Add just the first part of this param, since we will write the file data directly to the Stream
                    string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
                        boundary,
                        param.Key,
                        fileToUpload.FileName ?? param.Key,
                        fileToUpload.ContentType ?? "application/octet-stream");
    
                    formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
    
                    // Write the file data directly to the Stream, rather than serializing it to a string.
                    formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
                }
                else
                {
                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                        boundary,
                        param.Key,
                        param.Value);
                    formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
                }
            }
    
            // Add the end of the request.  Start with a newline
            string footer = "\r\n--" + boundary + "--\r\n";
            formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));
    
            // Dump the Stream into a byte[]
            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();
    
            return formData;
        }
    
     
  2. VoidITSolutions

    VoidITSolutions BANNED BANNED

    Joined:
    Apr 5, 2013
    Messages:
    164
    Likes Received:
    44
    I'm more into C/C++ myself but a quick google turned this up:
    http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data

    Stack Overflow is your best friend regarding the C family of languages.
    If that post doesn't help, search more there. You're bound to find something that will.
    Good luck!
     
  3. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    Take a look at the source code to RestSharp
     
  4. kytro360

    kytro360 Power Member

    Joined:
    Jan 12, 2010
    Messages:
    703
    Likes Received:
    732
    Do you have an example code of how to login to a website for a example that doesn't have an API? I looked at their examples and all have them putting in an API which the site I want to automate doesn't have.
     
  5. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page: