tags:

views:

2730

answers:

3

Hey, I'm new at using the the libraries WebClient, HttpResponse and HttpRequest in C#, so bare over with me, if my question is confusing to read.

I need to build a WinForm based on C# which can open a URL, which is secured with the basic authorization. I did this with adding this to the header, like this:

        using (WebClient wc = new WebClient())
        {
           wc.Headers.Add(HttpRequestHeader.Authorization, "Basic " +
           Convert.ToBase64String(
           Encoding.ASCII.GetBytes(username + ":" + password)));
        }

So fare, so good! Now I would like to fill a form with a number, and I find the source-code from the site, and discover that the name is "number". So I write this:

         NameValueCollection formData = new NameValueCollection();  
         formData["number"] = number
         byte[] responseBytes = wc.UploadValues(theurl, "POST", formData);
         string response = Encoding.ASCII.GetString(responseBytes);
         textBox_HTML.Text = response;

But how do I submit this? I will like to receive my "result-search"...

+6  A: 

You should probably be using HttpWebRequest for this. Here's a simple example:

var strId = UserId_TextBox.Text;
var strName = Name_TextBox.Text;

var encoding=new ASCIIEncoding();
var postData="userid="+strId;
postData += ("&username="+strName);
byte[]  data = encoding.GetBytes(postData);

var myRequest =
  (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
var result = responseReader.ReadToEnd();
BFree
Hungarian Notation and over use of var. Reminds me of the old Visual Basic days.
George Stocker
-1 for var usage in code example
rick schott
So downvoting someone who took the time to write out a code sample using a coding style you don't like is *more* helpful than actually providing an answer. Interesting....
BFree
I disagree with you guys. I can look at every one of those vars and tell you what they are without an IDE.
Samuel
@_rick_schott - that is petty and unhelpful. var does not detract; don't fear it.
Marc Gravell
Thanks for the answer.I have no problem using the HttpWebrequest, but the way I see it, nothing in the code fills out a search-form and submits this.This is my main problem.
Just replace the postData variable with the names of the form fields you need in your case. I believe you said it's number. So it would be something like " Then, if everything works correctly, the "result" variable will have the entire HTML of the search results page. At that point you can use Regex to parse the page and get what u neeed.
BFree
@Marc Gravell Using var in everything isn't always a good idea; especially for neophytes.
George Stocker
using var with polymorphic classes and constructors can be confusing to people who are not intimate with the API
rick schott
It's a very good answer, var and notation is irrelevant.
tomaszs
I don't usually use Hungarian Notation either, I must have copied and pasted that code when I answered this. As for var, I use it alot, and I will defend it!!!
BFree
A: 

You submitted it already with UploadValues. The question is: what is your "result-search"? What does the page return? HTML? If so - the HTML Agility Pack is the easiest way to parse html.

Marc Gravell
If I already have submitted with UploadValues, why does the "response" contain the exactly same code, as if don't use UploadValues? And how does the code know, what button the HTML-page is designed to submit with?
I would guess that there are other required form elements. Use Fiddler to trace the actual request from the server and inspect the form contents. Re "what button"... there *is* no button in a request; that is a UI device, that the browser translates into a GET/POST/etc.
Marc Gravell
Sorry, I'm confused about the problem, because I have a hard time figuring out, how I fill out a simple form on a page. The resulting page returns a cgi-page, which contains simpel up table which infomation.
+1  A: 

I have found a solution to my problem. First of all I was confused about some of the basics in http-communication. This was caused by a python-script i wrote, which have a different approach with the communication.

I solved it with generating the POST-data from scratch and open the uri, which was contained in the form-action.