Post to web page text box
Ive searched all of google and i cant seem to find the method for doing this when it seems so simple! All i have is a text box on a webpage, and given the text box id i want to write a value in it and press a button. So far i have:
WebRequest request = WebRequest.Create("http://ps766677/TestWebApplication/Default.aspx");
WebProxy proxy = new WebProxy("455646:8980");
request.Proxy = proxy;
request.Credentials = Credentia开发者_如何学ClCache.DefaultNetworkCredentials;
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = request.GetRequestStream();
string postData = GetData();
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
But the actual 'GetData' method is lost on me! Can anyone tell me what the GetData method should contain, im assuming returning a string of some sort of name value collection...
The postData
should contain the string of POST data that the server is expecting. You can download Tamper Data, and it will allow you to see what the server is expecting. You take those variables(and values) and append them together with the &
, and that will be your postData
string.
An example would be:
The server is expecting UserName
and Password
to come as POST data. Your string would look like...
string postData = string.Format("UserName={0}&Password={1}", "someUserName", "somePassword");
If you use Watin and Watin Test Recorder you could implement this automatically and then grab the C# outputted script as a reference
精彩评论