WebRequest not working as it should

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);
}
}
}
}
 
G

Guest

If the server recorded the "punch" then you must be using the correct URL.
If the response from the is incorrect, why would you assume you're doing
something wrong?

Try installing something in Ethereal and tracing the network traffic for the
communications in the two methods to see if you can see a difference.

It could be that the vbscript (assuming you're running it through IE) has
some cookies or URL state that don't have in your code. It could also have
something to do with the server detecting the capabilities of the client
based on the user agent string. All those you can see in the Ethereal logs
for comparison.
 
S

S Moran

thanks... no, listen...
the vbscript that works is just a VBS file using Microsoft.XMLHTTP object.

heres the deal... if you manually go to the url there are 2 textboxes and a
button (username, password). thats the logon punch. once you submit the form
your punch is recorded and the resulting page displays "Punch Recorded"

if i run this code with vbscript and capture the response and write it to an
html file, the file correctly show the resulting page with the "Punch
recorded" string.

when i run the c# code below, the punch IS recorded (i can verify with the
eTime team) but if i write the result from the server to an html file, this
time the page is the initial logon page and NOT the "punch recorded" page.

i assumed i was doing something in the wrong order but every example ive
seen suggests im doing it right.

any thoughts?



Peter Ritchie said:
If the server recorded the "punch" then you must be using the correct URL.
If the response from the is incorrect, why would you assume you're doing
something wrong?

Try installing something in Ethereal and tracing the network traffic for
the
communications in the two methods to see if you can see a difference.

It could be that the vbscript (assuming you're running it through IE) has
some cookies or URL state that don't have in your code. It could also
have
something to do with the server detecting the capabilities of the client
based on the user agent string. All those you can see in the Ethereal
logs
for comparison.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


S Moran said:
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);
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top