Wednesday, August 10, 2011

WebRequest and WebResponse Examples

As there are lot's of contents to talk about in C# network programming, in this article I am going to talk only a bit on the WebRequest/WebResponse briefly.

In this article I am going to talk about something on WebRequest, WebResponse method. I will talk about the WebRequest Method property to be "GET", "POST", "HEAD" et al to Get or Post a uri or get the Header contents. For the "POST", I am going to write some examples on the context type to be "text/xml" or "application/x-www-form-urlencoded" types. I am also going to write some sample code to talking about create a Webrequest object to call a SiteMinder protected resources.


The simplest example use "GET" method to request a uri:
eg.

var request=WebRequest.Create("http://...");
We can set the request properties, such as:
request.Credentials=CredentialCache.DefaultCredentials;
sometimes need use network credentials:(request.Credentials=new NetworkCredential(UserName, Password))
Sometimes we need to specify the Authentication lvl, such as:
request.AuthenticationLevel=System.Net.Security.AuthenticationLevel.MutualAuthRequested;

For the AuthenticationLevel, it can be None, MutualAuthRequested, MutualAuthRequired. which means no authentication, need client side authentication, and both server and client sides authentication mechanism.




No comments: