Wednesday, July 23, 2008

WPF experience - different stages

It seems to me that the experience could be split into several different stages when you work with WPF.

1. You are amazed: You type xml tags and something shows up! Cool! And those animations - ultra cool!!!

2. Trying to develop...a lot...gain experience....and still happy :)

3. You begin to understand how things work, you learn what the ItemsControl is from inside, you understand how to implement the application in a "correct" way and know how are you expected to use the framework in a right way. Now you are a bit disappointed because of the limitations - the WPF does not handle everything...

4. You implement something that is....incompatible with WPF. WPF is not as great as before, or you even start hating it.

5. You understand all benefits and limitations of WPF and start using its best parts and omit bad parts. You finally recall what the development is about (some think that it is about the usage of a technology and not about making the product). Life becomes better :)

6. You are really happy with what you are doing; you make the product and do not worry about the technology limitations.

7. You are ready to move to the next technology to be able to mix them in future.

Seems that I am currently on stage 6 and WPF is really great when you need GUI :) 

Monday, July 21, 2008

GET, POST and fetching HTTP with HttpWebRequest

Once upon a time there was a HttpWebRequest.  He tried to GET some HTML from YouTube to feed his web browser. HttpWebRequest had default settings and only the URL was unique, so YouTube said - Go request yourself, Bad Request 400!

I have done some basic investigation and found out that usually it is important to provide the Referrer field. So when I added that field  (request.Referrer = url;) - the request worked fine for the moment...

But later I decided that I would like to POST some data to the site...I set the request method to POST (request.Method = "POST";), set content type to "multipart/form-data", specified multipart boundary and updated ContentLength (request.ContentLength = xxx;). I opened the request stream (request.GetRequestStream();) and started writing POST form data to it (and calling stream.Flush() from time to time).

After I sent the request I needed the response, so I called request.GetResponse() and read out all data from the stream it returned. (BTW - do not forget to close streams :) )

I noticed that the data is not correct, or at least some part of it is missing (the one at the beginning), so I proceeded with the investigation.

I found out that the header Expect is set to 100-continue, and the web site was against that approach. So I needed to turn it off - I used such setting : request.ServicePoint.Expect100Continue = false;

Usually I also set the UserAgent field and Accept field.