473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

WebRequest not working as it should

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

Jul 10 '07 #1
1 2153
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" wrote:
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);
}
}
}
}
Jul 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Terry | last post by:
Hello, I am trying to get a response for an .aspx page in my current project (same virtual directory) by using WebRequest.GetResponse but I keep getting a exception with "500 Internal server...
12
by: ThyRock | last post by:
I am working on a WebRequest accessing the US Postal Service WebTools test API. This service uses a DLL file (ShippingAPITest.dll) with a query string which includes XML. The web service accepts...
5
by: james.dixon | last post by:
Hi I have been struggling with what should be a simple thing. I want to crawl the web for specific links, and save any html and files that meet my criteria to my hard drive. I thought that...
1
by: David Satz | last post by:
Hello--I just upgraded to Visual Studio .NET 2005 and suddenly, all my .NET 1.1 applications that accessed Web sites have broken. For example, this code: WebClient wc = new WebClient();...
6
by: Jack | last post by:
I have a WebRequest object that I use to log into a site and then post some XML. In doing this I set the KeepAlive = true so that it maintains the connection and does operates undo the initial...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm trying to create a call to a web page to validate and register software. The code I'm using is: Private Sub OK_Click(ByVal sender As...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
3
by: Dave | last post by:
string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Which works...
4
by: CindyH | last post by:
Hi I'm trying to use webrequest - webresponse to post a stream. I have set up a simple test with one aspx form holding the post code and trying to get another aspx form to receive the post on...
4
by: CindyH | last post by:
Hi - hope someone can help with this - this code was working for a while in the 'real' code and then suddenly stopped - not sure what happen. I made two simple forms on localhost to try to test...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.