<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dotnet thoughts &#187; HttpWebRequest</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/httpwebrequest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Post data using HttpWebRequest in C#</title>
		<link>http://www.dotnetthoughts.net/2009/11/10/post-data-using-httpwebrequest-in-c-sharp/</link>
		<comments>http://www.dotnetthoughts.net/2009/11/10/post-data-using-httpwebrequest-in-c-sharp/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 18:50:16 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[HttpWebRequest]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=633</guid>
		<description><![CDATA[While working around twitter or any other REST API services, you may require to do a post request to web server. If you are using .net can do this either using WebClient class or using HttpWebRequest class. Both these classes &#8230; <a href="http://www.dotnetthoughts.net/2009/11/10/post-data-using-httpwebrequest-in-c-sharp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While working around twitter or any other REST API services, you may require to do a post request to web server. If you are using .net can do this either using WebClient class or using HttpWebRequest class. Both these classes exists in System.Net namespace. I am using HttpWebRequest here because HttpWebRequest gives you more fine grained control over your request. WebClient does not. Also webclient encapsulates most of the stuff for you. HttpWebRequest allows to send headers or other customizations.</p>
<p>You can also invoke Web Services using HttpWebRequest class.</p>
<pre class="brush: csharp; title: ; notranslate">
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create(&quot;http://localhost/DemoApp/Default.aspx&quot;) as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = &quot;POST&quot;;
//Data to Post to the Page, itis key value pairs; separated by &quot;&amp;&quot;
string data = &quot;Username=username&amp;password=password&quot;;
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = &quot;application/x-www-form-urlencoded&quot;;
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
    MessageBox.Show(sr.ReadToEnd());
}
</pre>
<p>It will show some HTML content in the Message box. </p>
<p>You can also access or post data to .net web services. For sending data to webservices, you need to change the URL like following</p>
<p>http://www.dotnetthoughts.net/sample.asmx/HelloWorld.</p>
<p>where HelloWorld is the Web Method name to invoke. It will return an XML after successful operation.</p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/11/26/how-to-upload-file-using-httpwebrequest-class/" title="Permanent link to How to upload file using HttpWebRequest class">How to upload file using HttpWebRequest class</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/05/19/loading-image-from-url-in-windows-forms/" title="Permanent link to Loading Image from URL in Windows Forms">Loading Image from URL in Windows Forms</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/" title="Permanent link to How to check remote file exists using C#">How to check remote file exists using C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2011/04/01/how-to-post-data-to-restful-wcf-service/" title="Permanent link to How to post data to RESTful WCF service">How to post data to RESTful WCF service</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/11/10/post-data-using-httpwebrequest-in-c-sharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

