Advertisement
Guest User

chakrit

a guest
Dec 15th, 2008
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 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 Snowball.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.                 EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
  112.             }
  113.             string result = PostData(m_url, parameters.ToString());
  114.             return result;
  115.         }
  116.         /// <summary>
  117.         /// Posts the supplied data to specified url.
  118.         /// </summary>
  119.         /// <param name="url">The url to post to.</param>
  120.         /// <returns>a string containing the result of the post.</returns>
  121.         public string Post(string url)
  122.         {
  123.             m_url = url;
  124.             return this.Post();
  125.         }
  126.         /// <summary>
  127.         /// Posts the supplied data to specified url.
  128.         /// </summary>
  129.         /// <param name="url">The url to post to.</param>
  130.         /// <param name="values">The values to post.</param>
  131.         /// <returns>a string containing the result of the post.</returns>
  132.         public string Post(string url, NameValueCollection values)
  133.         {
  134.             m_values = values;
  135.             return this.Post(url);
  136.         }
  137.         /// <summary>
  138.         /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
  139.         /// </summary>
  140.         /// <param name="postData">The data to post.</param>
  141.         /// <param name="url">the url to post to.</param>
  142.         /// <returns>Returns the result of the post.</returns>
  143.         private string PostData(string url, string postData)
  144.         {
  145.             HttpWebRequest request = null;
  146.             if (m_type == PostTypeEnum.Post) {
  147.                 Uri uri = new Uri(url);
  148.                 request = (HttpWebRequest)WebRequest.Create(uri);
  149.                 request.Method = "POST";
  150.                 request.ContentType = "application/x-www-form-urlencoded";
  151.                 request.ContentLength = postData.Length;
  152.                 using (Stream writeStream = request.GetRequestStream()) {
  153.                     UTF8Encoding encoding = new UTF8Encoding();
  154.                     byte[] bytes = encoding.GetBytes(postData);
  155.                     writeStream.Write(bytes, 0, bytes.Length);
  156.                 }
  157.             } else {
  158.                 Uri uri = new Uri(url + "?" + postData);
  159.                 request = (HttpWebRequest)WebRequest.Create(uri);
  160.                 request.Method = "GET";
  161.             }
  162.             string result = string.Empty;
  163.             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
  164.                 using (Stream responseStream = response.GetResponseStream()) {
  165.                     using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8)) {
  166.                         result = readStream.ReadToEnd();
  167.                     }
  168.                 }
  169.             }
  170.             return result;
  171.         }
  172.         /// <summary>
  173.         /// Encodes an item and ads it to the string.
  174.         /// </summary>
  175.         /// <param name="baseRequest">The previously encoded data.</param>
  176.         /// <param name="dataItem">The data to encode.</param>
  177.         /// <returns>A string containing the old data and the previously encoded data.</returns>
  178.         private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
  179.         {
  180.             if (baseRequest == null) {
  181.                 baseRequest = new StringBuilder();
  182.             }
  183.             if (baseRequest.Length != 0) {
  184.                 baseRequest.Append("&");
  185.             }
  186.             baseRequest.Append(key);
  187.             baseRequest.Append("=");
  188.             baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement