Call webpage from program - c#

I would like my c# program to call a webpage when it opens. The code in the webpage will increment a counter... thus I just need the program to call the page. Don't think that I need to post or get or anything else.
Thoughts?

for more information check out msdn
// Create a request for the URL.
WebRequest request = WebRequest.Create (
"http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams and the response.
reader.Close ();
response.Close ();
if you can't extract the code you need from the above.. here it is
WebRequest request = WebRequest.Create ("http://www.mysite.com/counter.php?YourId");
WebResponse response = request.GetResponse();
response.Close ();

I know its late but for future people who might fall onto this.
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadDataAsync(new Uri("http://stackoverflow.com"));

Related

WebRequest to post data in Login page

I read questions with similar titles and test codes but my problem is not solved.
My problem: I need a program in C# that post dates to a modem login page. My modem is wimax bm632. The address of login page is htp://192.168.1.1/ and the action of form that there is
in http://192.168.1.1/ is http://192.168.1.1/index/login.cgi (form action). I want when login is success then go to http://192.168.1.1/html/wimax/security.asp and get page source.
login page : http://chamalz.persiangig.com/image/1.JPG
I write this code:
WebRequest request = WebRequest.Create("http://192.168.1.1/index/login.cgi");
request.Method = "POST";
string postData = "Username=admin&Password=YWRtaW4%3D";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
textBox1.Text=(((HttpWebResponse)response).StatusD escription);
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
textBox1.Text= (responseFromServer);
reader.Close ();
dataStream.Close ();
response.Close ();
This code has no error but have a problem : when I post my user name and password correct or incorrect I received one response in textBox1.Text
the response is : http://chamalz.persiangig.com/image/2.JPG
Please help me.
First you need to know what data are sent to login page via POST Method.
for achieving this you must login manualy and capture posted data to login page
this possible by using http capture software like httpdebugger
after you find correct post data and httpheader you can put data on your code and try login programaticaly

How to login to the android market using C#?

I would like to login to the webpage Android Market using C#. I spent the whole day reading about HTTP requests and POST data, but nothing seems to work. What I can do is read the webpage that holds the google login form. But reading the page AFTER the login seems impossible...
Can anyone give me a hint on how to do this?
BTW: The code I've tried is shown below:
string mail = "XXXXXXXXXX#gmail.com";
string pw = "XXXXXXXXXX";
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create (#"https://market.android.com/publish");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = String.Format ("Email={0}&Passwd={1}&signIn=Sign+in", mail, pw);
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
What I usually do: get live http headers for firefox and you can record the request your browser sends. Then simply repeat that in code.
for example, the android marketplace does it pretty different from your code.
it actually posts to: https://accounts.google.com/ServiceLoginAuth
And posts the data continue=https%3A%2F%2Fmarket.android.com%2Fpublish%2FHome&followup=https%3A%2F%2Fmarket.android.com%2Fpublish%2FHome&service=androiddeveloper&nui=1&dsh=&GALX=&pstMsg=1&dnConn=&timeStmp=&secTok=&Email=Passwd=&signIn=
You need to create a CookieContainer instance and assign it to the CookieContainer property in each request.
This will store the login cookie so that the site knows that you're still logged in.

How to get response from web API call

I have an API that I want to call and get the response back. Then I want to assign that response to a variable.
I have an API like:
http://example.org/Webservicesms_get_userbalance.aspx?user=xxxx&passwd=xxxx
When I run this URL in a browser, it prints an SMS balance. I want this SMS balance as a response and then assign it to a variable in my page.
You may also use WebRequest class.
WebRequest request = WebRequest.Create ("http://domainname/Webservicesms_get_userbalance.aspx?user=xxxx&passwd=xxxx");
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();
Of course, you can change Console.WriteLine to whatever you want to do with response.
Your service is an .aspx page which just returns text (the sms balance) and no html?
If so you can 'scrape it'
string urlData = String.Empty;
WebClient wc = new WebClient();
urlData = wc.DownloadString("http://domainname/Webservicesms_get_userbalance.aspx?user=xxxx&passwd=xxxx");

finding c# ways to send http requests

I am trying to develop a Microsoft excel plugin to send excel sheet data to a web application. It will require the plugin to prompt username and password and then send login http request to the web application to get a session . Then it will upload data to the web application . What .net things should I use ?
Post Method Sample for send username and password. for uploading file just search "File Upload C#" in google.com or bing.com or try C#'s WebClient.UploadFile, Code Project
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://example.com");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "username=user&passsword=pass";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close();
For HTTP communication in .NET, try System.Net.WebClient or System.Net.HttpWebRequest.

Making http request from code behind

I am trying to implement Message API
I am not sure how I will be calling this from code-behind and in their snippet it says:
https://platform.3cinteractive.com/api/send_message.php
POST
username=aRDSe3vcaMzh06YrMcxcQw==&password=1BSvQc6lpNlnp4ufWgRLPHNJ7RMrL8CcaWCzL1Vtw+Y=&phone_number=+11234567890&trigger_id=1105&message=howdy
Use the WebRequest class below or use a library like RestSharp for more control or your HTTP request:
// Create a request for the URL.
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.
Console.WriteLine (response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();
You need to use a WebRequest and do an HTTP POST. See this article entitled How to: Send Data Using the WebRequest Class.

Categories