Jerry8989

I have to access a web page and click on a specific button that will allow that site to upload a bunch of my files.

I want to just use the HttpWebRequest and HttpWebResponse but I don't want to use a browser control or class if possible.

I have code that will read in the page to make sure the exact button name exists and if it does I want to be able to click that button.

Please let me know if more info is needed

Thank You

Jerry



Re: Windows Forms General How to mimic a button click within a web page

OmegaMan

Does the action of the button do more than a simple upload Once you know the Url, one should be able to push the files to the target on the Server using http/request/response.





Re: Windows Forms General How to mimic a button click within a web page

Jerry8989

The button will allow the server that hosts the page to promote my files from my site generation server to the live server. All I need to do is replicate a user clicking on the button that will allow the promotion. Users are able to go to this site and click the button but I can't have someone do it daily it has to be automated.

Thank You

Jerry





Re: Windows Forms General How to mimic a button click within a web page

OmegaMan

See if this post works within your constraints:

Pressing button on a web page form





Re: Windows Forms General How to mimic a button click within a web page

Jerry8989

Omega,

Thanks for your replies. I'm trying to get this to work without using the webbrowser control.

I think I just need to know how to use the HTTPWebResponse to post what the button is posting.

I've never used it before so if you have any ideas I would greatly apprciate it.

Thank You

Jerry





Re: Windows Forms General How to mimic a button click within a web page

Yu Guo – MSFT

Hi, Jerry8989,

If you don't like WebBrowser control, you can use HttpWebRequest to do your job.

But first of all, you should learn some html basics.

In fact, when you click the Button in a webpage, it submit a Web Form to the Web Server.

http://www.w3.org/TR/html4/interact/forms.html

And the file upload tag is among one of the input tags.

If you check the souce code of a file upload page, you may find something like

<form action ="http://localhost/test.php" method ="POST">
<input type="text" name="uname">
<input type="password" name="passwd">
<input type="FILE" name="uploadfile">
<input type="submit"> //This is the button you mentioned
</form>

So, you should do some research of the webpage and find out all the useful elements.

And this sample could be helpful to achieve your goal.

http://www.codeproject.com/csharp/UploadFileEx.asp

Hope this helps,

Regards






Re: Windows Forms General How to mimic a button click within a web page

Jerry8989

Yu Guo,

Thank you for your post.

Here is the code this is used to create the button I need to click.

This code is on the clients machine and it is developed by them. I have no control over their code. I just want to automate the pressing of this button without using a WebBrowser control. The code on their end does all the uploading of my files. I don't have to do anything except click that button.

I understand how to post data to the web server but the button click has been harder to figure out. Unless I'm totally missing your point.

<FORM style="MARGIN-BOTTOM: 0px" action=/servlet/xxxxxx/wwwwww method=post>

<INPUT style="BACKGROUND-COLOR: #bcd8f1" type=submit value="PreProd" name="PreProd"></FORM>

Thank you again for your help

Jerry





Re: Windows Forms General How to mimic a button click within a web page

Yu Guo – MSFT

Hi, Jerry8989,

I am not quite sure how do you select the upload files.

If it is just a click and upload, maybe the following codes could be helpful.

Code Block

ASCIIEncoding encoding = new ASCIIEncoding();

string postData = "PreProd=PreProd";

byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://yourservername/servlet/xxxxxx/wwwwww");

myRequest.Method = "POST";

myRequest.ContentType = "application/x-www-form-urlencoded";

myRequest.ContentLength = data.Length;

Stream newStream = myRequest.GetRequestStream();

// Send the data.

newStream.Write(data, 0, data.Length);

newStream.Close();

// Get response

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);

string content = reader.ReadToEnd();

Console.WriteLine(content);

Hope this helps,

Regards






Re: Windows Forms General How to mimic a button click within a web page

Jerry8989

Yu Guo,

I don't have to upload anything. All I have to do is click 1 button on a page. That is all that I have to do. All the upload code is done on the clients server





Re: Windows Forms General How to mimic a button click within a web page

Yu Guo – MSFT

Hi, Jerry8989,

Have you tried the codes in my last post

If there is no other security protection in that page, I think it should work for your purpose.

Regards






Re: Windows Forms General How to mimic a button click within a web page

Jerry8989

Yu Guo,

I did try that code above but it never fires off the button click. I created a test page and I used that code to try and go and click it and that button click creates a file. When I use that code it doesn't fire off the button.

Any other ideas Thanks for your help I really appricate you help.

Do you think WebClient.UploadValues would do the same thing

Jerry





Re: Windows Forms General How to mimic a button click within a web page

Yu Guo – MSFT

Hi, Jerry8989,

I think WebClient.UploadValues works just like HttpWebRequest.

And you can try it.

But could you tell what is the value of "content" after you click the button

Regards






Re: Windows Forms General How to mimic a button click within a web page

Jerry8989

Yu Guo,

The content returns the code for the page.

I created a test app using this code

<html enableviewstate=false>

<head runat="server" enableviewstate=false>

<title>Untitled Page</title>

</head>

<body enableviewstate=false>

<form id="form1" runat="server" method=post>

<div enableviewstate=false>

<asp:Button id=PreProd runat="server" Text="PreProd" OnClick="PreProd_Click" EnableViewState="False" />&nbsp;<br />

<asp:Label ID="Label1" runat="server" Height="470px" Text="Label" Width="634px"></asp:Label></div>

</form>

</body>

</html>

This is the C# code

protected void PreProd_Click(object sender, EventArgs e)

{

StreamWriter SW;

string filename = "c:\\CreatedTestFiles\\MyTextFile" + "_" + DateTime.Now.Ticks.ToString() + "_tempfile.txt";

SW = File.CreateText(filename);

SW.WriteLine("This is a test file.");

SW.WriteLine("This is second line");

SW.WriteLine("This is third line");

SW.Close();

Console.WriteLine("File Created Successfully");

}

-------------------------------------------------------------------------------------------------------------------------------------------------

The button click never fires using this code:

string webURL = "http://pdcdev01/Global%20Web%20Hosting/Global%20Web%20Hosting.aspx";

ASCIIEncoding encoding = new ASCIIEncoding();

string postData = "PreProd=";

byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webURL);

myRequest.Method = "POST";

myRequest.ContentType = "application/x-www-form-urlencoded";

myRequest.ContentLength = data.Length;

Stream newStream = myRequest.GetRequestStream();

// Send the data.

newStream.Write(data, 0, data.Length);

newStream.Close();

// Get response

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);

string content = reader.ReadToEnd();

reader.Close();

Console.WriteLine(content);

For the PostData i've used "PreProd=PreProd" and that didn't work either.

Thanks again for your help





Re: Windows Forms General How to mimic a button click within a web page

Yu Guo – MSFT

Hi, Jerry8989,

The problem is even if you set all enableviewstate to false, there are still some hidden input tags in the Web Form.

( You can right click the page and check the source.)

So, I wrote a sample to read all the input tags in this html, and it could help to solve this problem.

Code Block

Regex inputs = new Regex(@"<input[\s\S]* (</input>|/>)", RegexOptions.IgnoreCase);

Regex name = new Regex(@"name=""([^""]*)""", RegexOptions.IgnoreCase);

Regex value = new Regex(@"value=""([^""]*)""", RegexOptions.IgnoreCase);

Regex remove = new Regex(@"name=""|""|value=""", RegexOptions.IgnoreCase);

string webURL = "http://pdcdev01/Global%20Web%20Hosting/Global%20Web%20Hosting.aspx";

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webURL);

// Get response

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);

string content = reader.ReadToEnd();

reader.Close();

MatchCollection collection=inputs.Matches(content);

string[] inputStrs=new string[collection.Count];

string[] names = new string[collection.Count];

string[] values = new string[collection.Count];

for (int i = 0; i < inputStrs.Length; i++)

{

inputStrs[i] = collection[i].Value;

names[i] = name.Match(inputStrs[i]).Value;

names[i] = remove.Replace(names[i], "");

values[i] = value.Match(inputStrs[i]).Value;

values[i] = remove.Replace(values[i], "");

}

ASCIIEncoding encoding = new ASCIIEncoding();

string postData = HttpUtility.UrlEncode(names[0]) + "=" + HttpUtility.UrlEncode(values[0]);

for (int i = 1; i < names.Length; i++)

{

postData += "&" + HttpUtility.UrlEncode(names[i]) + "=" + HttpUtility.UrlEncode(values[i]);

}

byte[] data = encoding.GetBytes(postData);

myRequest = (HttpWebRequest)WebRequest.Create(webURL);

myRequest.Method = "POST";

myRequest.ContentType = "application/x-www-form-urlencoded";

myRequest.ContentLength = data.Length;

Stream newStream = myRequest.GetRequestStream();

// Send the data.

newStream.Write(data, 0, data.Length);

newStream.Close();

Hope this helps,

Regards






Re: Windows Forms General How to mimic a button click within a web page

covaisekar

Hi Yu Guo

My current assignment is with the screen scrapper module, so I need to do lot of button click, link click & inputting a text box values within a web page functionality what was discussed in the post, my doubt is this the same method of implementation will work it our for all kind of web pages like asp, html, php and some king of .cfm link enabled web pages.

I am very new to this module so I need your help in this to grape the knowledge in web page posting with data and making a button, link click in the page through program.

Thanks