Sending requests to a website is pretty easy with http. Https is simple as well, but there are some things that you are going to have to look out for. One of the first things that you will need is a NetworkCredential object to identify yourself.
NetworkCredential cred=new NetworkCredential("daeron", "mypassword");
I wouldn't recommend hardcoding the user or password, but this is for demo purposes ;).
Of course, you'll need to add the System.Net namespace and a reference to the dll.
using System.Net;
You create a webrequest (HttpWebRequest provides more http, https functionality):
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (https://www.mydomain.com/);
request.Credentials = new NetworkCredential("daeron", "mypassword");
Finally, you get the response:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
One hang up you will run into if the webserver is using Windows Integrated Authentication and you are not on that server's domain or one that the domain has trust with. You may get 401 unauthorized errors. Your fix? use UnsafeAuthenticatedConnection Sharing:
request.UnsafeAuthenticatedConnectionSharing = true;
This maintains the NTLMAuthenticated connection between requests. One thing to keep an eye out for is ensuring that you know exactly when this is used as it can be used to as it could potentially be used to connect to resources when the user is not authenticated. Make sure that you have checks before this request is called.
The full code is below:
using System;
using System.Net;
namespace HttpsDemo
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.mydomain.com");
request.Credentials = new NetworkCredential("daeron", "mypassword");
request.UnsafeAuthenticatedConnectionSharing = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string s = sr.ReadToEnd();
sr.Close();
Console.WriteLine(s);
}
}
}
Tuesday, October 21, 2008
Tuesday, September 30, 2008
Dynamic Where Clause in LINQ
I had a friend who wanted to do a dynamic where clause. The problem he ran into was adjusting the predicate when you don't know the properties that you are going to query. I have a solution that may be of help to you.
Suppose you want to do something like the following:
YourTypeDataContext db=new YourTypeDataContext();
var users=from u in db.Users where (dynamic) select u;
The problem is that the properties aren't immediately known. A solution is to use reflection like the following:
string dynamicProperty="DynamicPropertyName";
string val="valToSearchFor";
Type t=typeof(User); //User is a type generated by the dbml for the Users table
System.Reflection.PropertyInfo p=t.GetProperty(dynamicProperty);
users=users.Where( u=> Convert.ToString( p.GetValue( u, null ) ) == val);
The first thing I'm doing is getting the type of User so that I can reflect and get the property names. Then I'm setting a string (dynamicProperty) to any property I want to search for. A method that took this and the value to search for as parameters would probably be the best way for you to do this.
I wrapped Convert.ToString around t.GetProperty to account for null values, and set my IQueryable return value to the same variable and voila!
I will adjust this post with a more dynamic approach and account for multiple properties in the near future. Hope it helps!
Suppose you want to do something like the following:
YourTypeDataContext db=new YourTypeDataContext();
var users=from u in db.Users where (dynamic) select u;
The problem is that the properties aren't immediately known. A solution is to use reflection like the following:
string dynamicProperty="DynamicPropertyName";
string val="valToSearchFor";
Type t=typeof(User); //User is a type generated by the dbml for the Users table
System.Reflection.PropertyInfo p=t.GetProperty(dynamicProperty);
users=users.Where( u=> Convert.ToString( p.GetValue( u, null ) ) == val);
The first thing I'm doing is getting the type of User so that I can reflect and get the property names. Then I'm setting a string (dynamicProperty) to any property I want to search for. A method that took this and the value to search for as parameters would probably be the best way for you to do this.
I wrapped Convert.ToString around t.GetProperty to account for null values, and set my IQueryable return value to the same variable and voila!
I will adjust this post with a more dynamic approach and account for multiple properties in the near future. Hope it helps!
Subscribe to:
Posts (Atom)