473,657 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpWebRequest doesn't work with JavaScript ;-(

Hello all,

This is a little frustrating ;-(

I have the following used as part of Javascript code. I am trying to
send values from the client to the server following the information
from this web site: http://www.netomatix.com/HttpPostData.aspx

But what I have found is that the code below generates an error and
does not seem to work.
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest )
HttpWebRequest. Create("http://localhost:2255/Test.aspx");
myRequest.Metho d = "POST";
myRequest.Conte ntType="applica tion/x-www-form-urlencoded";
myRequest.Conte ntLength = data.Length;

Stream newStream=myReq uest.GetRequest Stream();
// Send the data.
newStream.Write (data,0,data.Le ngth);
newStream.Close ();
Please, please help! I am sooo tired! What am I doing wrong?

Regards.

Nov 19 '05 #1
5 1193
OK - I think the problem was that the code above was for C# and not
javascript? They both look the same ;-/

Anywho, I tried the following code below:

function sendDataToServe r (url, dataToPost) {
var httpRequest;
if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject(' Microsoft.XMLHT TP');
}
else if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest( );
}
if (httpRequest) {
httpRequest.ope n('POST', url, false);
httpRequest.set RequestHeader(' Content-Type',
'application/x-www-form-urlencoded');

alert("about to post the data");
httpRequest.sen d(dataToPost);
return httpRequest;
}
else {
return void 0;
}
}

And this seems to work - but now I am trying to get the values out on
the server side. I tried the following code below:

using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;

public partial class Test : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (IsPostBack)
{
//do whatever with the form data here
string firstName =
(string)Request .QueryString["FDepents44 "];
Console.WriteLi ne(firstName);
}
}
}

I used the debugger and found that "IsPostBack " is false so the snippet
of code is not being executed. So, how can one get the data out on the
server side using C#? Is the call from the client (using Javascript)
correct? I am just wondering because (as mentioned before) IsPostBack
is set to false.

TIA

Nov 19 '05 #2
Not done too much jiggerypokery with Javaqscript as far as programatically
posting like described here, but it would seem that POST and POSTBACK would
be two different beasts.

Looking at your code you make one POST without first having made a GET, so
it is hard to see how postback could occur.

I may be completely wrong here, and I am sure someone will no doubt correct
me.

Regards Mr N . . .
"milkyway" <d0******@hotma il.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
OK - I think the problem was that the code above was for C# and not
javascript? They both look the same ;-/

Anywho, I tried the following code below:

function sendDataToServe r (url, dataToPost) {
var httpRequest;
if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject(' Microsoft.XMLHT TP');
}
else if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest( );
}
if (httpRequest) {
httpRequest.ope n('POST', url, false);
httpRequest.set RequestHeader(' Content-Type',
'application/x-www-form-urlencoded');

alert("about to post the data");
httpRequest.sen d(dataToPost);
return httpRequest;
}
else {
return void 0;
}
}

And this seems to work - but now I am trying to get the values out on
the server side. I tried the following code below:

using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;

public partial class Test : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (IsPostBack)
{
//do whatever with the form data here
string firstName =
(string)Request .QueryString["FDepents44 "];
Console.WriteLi ne(firstName);
}
}
}

I used the debugger and found that "IsPostBack " is false so the snippet
of code is not being executed. So, how can one get the data out on the
server side using C#? Is the call from the client (using Javascript)
correct? I am just wondering because (as mentioned before) IsPostBack
is set to false.

TIA

Nov 19 '05 #3
Thanks for the pointer. Is there a correct way to write code on the
server side whenever a POST is being done from the client in C#? I
thought that one was to use the page_load somehow.

Also, when I am sending the string, it is in the form of:
TableName=FDepe nts&FDepents11= 600&FDepents12= 609 ....

I have now seen (through the debugger), that the following code:

string firstName =
(string)Request .QueryString.Ge t("TableName" );

Just returns a null, even though in the part of the code
alert("about to post the data");
httpRequest.sen d(dataToPost); <--- HERE
return httpRequest;

Clearly has the parameters being passed in..

Sigh - will wonders ever cease

Nov 19 '05 #4
The if( Page.IsPostback ) construct is the correct way to test if this is a
postback request; Its just that your code which generates the post to the
server is in some question. Having said that, as I have not done this, so I
cant comment on the vailidy of your approach other than one must have to
trick ( if possible ) ASP.NET into a normal mode of operation which would
make it think that this is postback and not the first GET.

As far as the code which your javscript passes to the receiving aspx page,
have you tried this manually ( IE Creating the URL on notepad or something
and pasting it into the browser address bar ). Dont forget to put the '?'
after the basic URL before your query string. Alternatively, there may be
some character stripping going on if this is going though a gateway or
something???!?? !??!?!

Let me know how you are getting on, as I find this topic interesting .

Regards Mr N . . . .

"milkyway" <d0******@hotma il.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Thanks for the pointer. Is there a correct way to write code on the
server side whenever a POST is being done from the client in C#? I
thought that one was to use the page_load somehow.

Also, when I am sending the string, it is in the form of:
TableName=FDepe nts&FDepents11= 600&FDepents12= 609 ....

I have now seen (through the debugger), that the following code:

string firstName =
(string)Request .QueryString.Ge t("TableName" );

Just returns a null, even though in the part of the code
alert("about to post the data");
httpRequest.sen d(dataToPost); <--- HERE
return httpRequest;

Clearly has the parameters being passed in..

Sigh - will wonders ever cease

Nov 19 '05 #5
Hi MrN,

I just decided to use the forms.submit since it did not work. Here is a
link to where I obtained the information, perhaps you can try:

http://groups.google.com/group/de.co...96931c799d7772

It is in German but one can understand enough and see the code below.
Here is another:

http://groups.google.com/group/micro...e7f72f2f4da835

Also, it was my understanding that this can only work with MS browsers
- soooo - I left it alone.

Kindest Regards.

Nov 19 '05 #6

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

Similar topics

5
12328
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker threads from the ThreadPool. However, eventually (relatively quickly) there become fewer and fewer available worker threads in the pool, until there are 0 and a System.InvalidOperationException occurs with the message: "There were not enough free...
10
472
by: Brian Brown | last post by:
I have code which works as an asp.net page that posts an xml file to web page and gets a response back. When the the calls GetResponse() it goes into the page it's posting to to and works fine. When it's been ported to a winform it doesn't work on the GetResponse() call. I think it probably needs credentials but not sure what to use, tried a few ids w/o any luck.
10
19342
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes the server happy. I set up a copy of this form at my web site so that I could see exactly what a...
5
11978
by: milkyway | last post by:
Hello all, This is a little frustrating ;-( I have the following used as part of Javascript code. I am trying to send values from the client to the server following the information from this web site: http://www.netomatix.com/HttpPostData.aspx But what I have found is that the code below generates an error and does not seem to work.
6
3081
by: omyek | last post by:
I'm trying to mimic the browsing of a webpage using an HttpWebRequest. I've had a lot of luck with it so far, including logging into pages, posting form data, and even collecting and using cookies. However, I ran into a scenario that I'm baffled by. I have a website which requires a user to login. This is nothing new and I was able to successfully log in. For our case, let's say the URL to the login page is http://login.html
2
6801
by: TK | last post by:
I have a trouble to get web resopnse from an aspx page which is secured by Forms Authentication with custom user account database. My client application is a console application but not a browser. I want to download a file from my webapplication. I've learned that the NetworkCredential class gives a way to go but no luck. My code is as following...just dump out the web response for debugging. // C# public void Download(string username,...
3
3951
by: Amil | last post by:
Please don't repond to this if you are guessing or just don't know the answer. I'm trying to login to a backend system running Java/Tomcat. I create a HttpWebRequest with the login data and do a POST. This works fine. The HttpWebResponse content I get back is just javascript "window.location=xxx" (with normal html around it). The HttpWebResponse also contains a java session id cookie. Fine so far. I want to go to the new location...
0
1333
by: Susan Van Houen | last post by:
Hi Everybody, I have a problem that is driving me crazy and I am hoping for some help here. I am building an application (vb.net, vs2003) that crawls selected websites. The application works fine except for one thing: Some sites rely on session cookies and I don't seem to be returning the cookies. The IIS5 log looks like this when I access the same page with
0
1554
by: boxboy | last post by:
Hi, I'm writing a console application and am having a problem with HttpWebRequest when posting data to a webserver. A "System.Net.WebException: The server committed a protocol violation" is always being thrown when getting the response from a specific host. I'm pretty sure the framework's implementation doesn't like the formating or status code of the returned response header. It throws the exception without giving me a way of handling it...
2
2735
by: Tosco | last post by:
I used to use WebBrowser in VB6, and now I want to learn how to use WebRequest in vb.net, but I'm having 2 problems. 1) Is it normal that the automatic redirection doesn't work when the page contains this? <body onLoad="location='http://www.SiteName.com/index.jsp'"></body> If I use WebBrowser.Navigate in VB6 with this redirection works.
0
8395
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.