S
S Moran
c# vs2005 pro
been asked to write a small app that "punches in" via a POST to a URL. if
the response from the server contains the text "Punch Recorded" then all is
well. Whats happening is that the punch IS being recorded but the response
doesnt look like what it should look like, so i must be doing something
wrong. if i dump the response to a file and look at it, its actually the
initial page that a user would go to to punch in, instead of the response
that you would get AFTER punching in. (btw this all works fine in vbscript)
code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace punch
{
class Program
{
static void Main(string[] args)
{
string userName = "username";
string passWord = "password";
Punch(userName, passWord);
}
static void Punch(string userName, string passWord)
{
string postData = "username=" + userName + "&password=" +
passWord;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
WebRequest request = WebRequest.Create("http://someurl.jsp");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string serverResponse = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
if (serverResponse.IndexOf("Recorded Time") < 0)
{
Console.WriteLine("Punch NOT recorded. Check username and
password.");
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
else
{
Console.WriteLine("Punch recorded.");
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
}
}
}
been asked to write a small app that "punches in" via a POST to a URL. if
the response from the server contains the text "Punch Recorded" then all is
well. Whats happening is that the punch IS being recorded but the response
doesnt look like what it should look like, so i must be doing something
wrong. if i dump the response to a file and look at it, its actually the
initial page that a user would go to to punch in, instead of the response
that you would get AFTER punching in. (btw this all works fine in vbscript)
code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace punch
{
class Program
{
static void Main(string[] args)
{
string userName = "username";
string passWord = "password";
Punch(userName, passWord);
}
static void Punch(string userName, string passWord)
{
string postData = "username=" + userName + "&password=" +
passWord;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
WebRequest request = WebRequest.Create("http://someurl.jsp");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string serverResponse = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
if (serverResponse.IndexOf("Recorded Time") < 0)
{
Console.WriteLine("Punch NOT recorded. Check username and
password.");
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
else
{
Console.WriteLine("Punch recorded.");
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
}
}
}