Guest User

Untitled

a guest
Nov 23rd, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Web;
  5. using System.Net;
  6. using System.Collections.Specialized;
  7.  
  8. namespace POST_1.common
  9. {
  10.     /// <summary>
  11.     /// Submits post data to a url.
  12.     /// </summary>
  13.     public class PostSubmitter
  14.     {
  15.         /// <summary>
  16.         /// determines what type of post to perform.
  17.         /// </summary>
  18.         public enum PostTypeEnum
  19.         {
  20.             /// <summary>
  21.             /// Does a get against the source.
  22.             /// </summary>
  23.             Get,
  24.             /// <summary>
  25.             /// Does a post against the source.
  26.             /// </summary>
  27.             Post
  28.         }
  29.  
  30.         private string m_url = string.Empty;
  31.         private NameValueCollection m_values = new NameValueCollection();
  32.         private PostTypeEnum m_type = PostTypeEnum.Get;
  33.         /// <summary>
  34.         /// Default constructor.
  35.         /// </summary>
  36.         public PostSubmitter()
  37.         {
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Constructor that accepts a url as a parameter
  42.         /// </summary>
  43.         /// <param name="url">The url where the post will be submitted to.</param>
  44.         public PostSubmitter(string url)
  45.             : this()
  46.         {
  47.             m_url = url;
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Constructor allowing the setting of the url and items to post.
  52.         /// </summary>
  53.         /// <param name="url">the url for the post.</param>
  54.         /// <param name="values">The values for the post.</param>
  55.         public PostSubmitter(string url, NameValueCollection values)
  56.             : this(url)
  57.         {
  58.             m_values = values;
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Gets or sets the url to submit the post to.
  63.         /// </summary>
  64.         public string Url
  65.         {
  66.             get
  67.             {
  68.                 return m_url;
  69.             }
  70.             set
  71.             {
  72.                 m_url = value;
  73.             }
  74.         }
  75.         /// <summary>
  76.         /// Gets or sets the name value collection of items to post.
  77.         /// </summary>
  78.         public NameValueCollection PostItems
  79.         {
  80.             get
  81.             {
  82.                 return m_values;
  83.             }
  84.             set
  85.             {
  86.                 m_values = value;
  87.             }
  88.         }
  89.         /// <summary>
  90.         /// Gets or sets the type of action to perform against the url.
  91.         /// </summary>
  92.         public PostTypeEnum Type
  93.         {
  94.             get
  95.             {
  96.                 return m_type;
  97.             }
  98.             set
  99.             {
  100.                 m_type = value;
  101.             }
  102.         }
  103.         /// <summary>
  104.         /// Posts the supplied data to specified url.
  105.         /// </summary>
  106.         /// <returns>a string containing the result of the post.</returns>
  107.         public string Post()
  108.         {
  109.             StringBuilder parameters = new StringBuilder();
  110.             for (int i = 0; i < m_values.Count; i++)
  111.             {
  112.                 EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
  113.             }
  114.             string result = PostData(m_url, parameters.ToString());
  115.             return result;
  116.         }
  117.         /// <summary>
  118.         /// Posts the supplied data to specified url.
  119.         /// </summary>
  120.         /// <param name="url">The url to post to.</param>
  121.         /// <returns>a string containing the result of the post.</returns>
  122.         public string Post(string url)
  123.         {
  124.             m_url = url;
  125.             return this.Post();
  126.         }
  127.         /// <summary>
  128.         /// Posts the supplied data to specified url.
  129.         /// </summary>
  130.         /// <param name="url">The url to post to.</param>
  131.         /// <param name="values">The values to post.</param>
  132.         /// <returns>a string containing the result of the post.</returns>
  133.         public string Post(string url, NameValueCollection values)
  134.         {
  135.             m_values = values;
  136.             return this.Post(url);
  137.         }
  138.         /// <summary>
  139.         /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
  140.         /// </summary>
  141.         /// <param name="postData">The data to post.</param>
  142.         /// <param name="url">the url to post to.</param>
  143.         /// <returns>Returns the result of the post.</returns>
  144.         private string PostData(string url, string postData)
  145.         {
  146.             HttpWebRequest request = null;
  147.             if (m_type == PostTypeEnum.Post)
  148.             {
  149.                 Uri uri = new Uri(url);
  150.                 request = (HttpWebRequest)WebRequest.Create(uri);
  151.                 request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
  152.                 request.Method = "POST";
  153.                 request.ContentType = "application/x-www-form-urlencoded";
  154.                 request.ContentLength = postData.Length;
  155.                 using (Stream writeStream = request.GetRequestStream())
  156.                 {
  157.                     UTF8Encoding encoding = new UTF8Encoding();
  158.                     byte[] bytes = encoding.GetBytes(postData);
  159.                     writeStream.Write(bytes, 0, bytes.Length);
  160.                 }
  161.             }
  162.             else
  163.             {
  164.                 Uri uri = new Uri(url + "?" + postData);
  165.                 request = (HttpWebRequest)WebRequest.Create(uri);
  166.                 request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
  167.                 request.Method = "GET";
  168.             }
  169.             string result = string.Empty;
  170.  
  171.             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  172.             {
  173.                 using (Stream responseStream = response.GetResponseStream())
  174.                 {
  175.                     using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
  176.                     {
  177.                         result = readStream.ReadToEnd();
  178.                     }
  179.                 }
  180.             }
  181.             return result;
  182.             return null;
  183.         }
  184.         /// <summary>
  185.         /// Encodes an item and ads it to the string.
  186.         /// </summary>
  187.         /// <param name="baseRequest">The previously encoded data.</param>
  188.         /// <param name="dataItem">The data to encode.</param>
  189.         /// <returns>A string containing the old data and the previously encoded data.</returns>
  190.         private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
  191.         {
  192.             if (baseRequest == null)
  193.             {
  194.                 baseRequest = new StringBuilder();
  195.             }
  196.             if (baseRequest.Length != 0)
  197.             {
  198.                 baseRequest.Append("&");
  199.             }
  200.             baseRequest.Append(key);
  201.             baseRequest.Append("=");
  202.             baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
  203.         }
  204.     }
  205. }
  206.  
Advertisement
Add Comment
Please, Sign In to add comment