Click here to Skip to main content
15,893,508 members
Articles / Code generation

Semi generated crawler

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Jun 2012CPOL4 min read 21.6K   599   10  
Leverage Visual studio Web Test Framework for your crawling needs...
using System;
using System.IO;
using System.Text;
namespace LightWebTestFramework
{
	[Serializable]
	public class StringHttpBody : IHttpBody, ICloneable
	{
		internal const string ContentTypeJSON = "application/json";
		private string m_contentType;
		private string m_bodyString = string.Empty;
		private bool m_insertByteOrderMark;
		public string BodyString
		{
			get
			{
				return this.m_bodyString;
			}
			set
			{
				this.m_bodyString = value;
			}
		}
		public string ContentType
		{
			get
			{
				return this.m_contentType;
			}
			set
			{
				this.m_contentType = value;
			}
		}
		public bool InsertByteOrderMark
		{
			get
			{
				return this.m_insertByteOrderMark;
			}
			set
			{
				this.m_insertByteOrderMark = value;
			}
		}
		public void WriteHttpBody(WebTestRequest request, Stream bodyStream)
		{
			if (this.m_bodyString == null)
			{
				return;
			}
			if (this.InsertByteOrderMark)
			{
				Encoding encoding = request.Encoding;
				if (request.Encoding.EncodingName.Equals(Encoding.UTF8.EncodingName, StringComparison.OrdinalIgnoreCase))
				{
					encoding = Encoding.UTF8;
				}
				else
				{
					if (request.Encoding.EncodingName.Equals(Encoding.UTF32.EncodingName, StringComparison.OrdinalIgnoreCase))
					{
						encoding = Encoding.UTF32;
					}
					else
					{
						if (request.Encoding.EncodingName.Equals(Encoding.Unicode.EncodingName, StringComparison.OrdinalIgnoreCase))
						{
							encoding = Encoding.Unicode;
						}
					}
				}
				using (StreamWriter streamWriter = new StreamWriter(bodyStream, encoding))
				{
					streamWriter.Write(this.m_bodyString);
					return;
				}
			}
			byte[] bytes = request.Encoding.GetBytes(this.m_bodyString);
			bodyStream.Write(bytes, 0, bytes.Length);
		}
		public object Clone()
		{
			return (StringHttpBody)base.MemberwiseClone();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions