473,386 Members | 1,652 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,386 software developers and data experts.

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.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
Please, please help! I am sooo tired! What am I doing wrong?

Regards.

Nov 19 '05 #1
5 1174
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 sendDataToServer (url, dataToPost) {
var httpRequest;
if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
else if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
if (httpRequest) {
httpRequest.open('POST', url, false);
httpRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

alert("about to post the data");
httpRequest.send(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.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//do whatever with the form data here
string firstName =
(string)Request.QueryString["FDepents44"];
Console.WriteLine(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******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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 sendDataToServer (url, dataToPost) {
var httpRequest;
if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
else if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
if (httpRequest) {
httpRequest.open('POST', url, false);
httpRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

alert("about to post the data");
httpRequest.send(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.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//do whatever with the form data here
string firstName =
(string)Request.QueryString["FDepents44"];
Console.WriteLine(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=FDepents&FDepents11=600&FDepents12=609 ....

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

string firstName =
(string)Request.QueryString.Get("TableName");

Just returns a null, even though in the part of the code
alert("about to post the data");
httpRequest.send(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******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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=FDepents&FDepents11=600&FDepents12=609 ....

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

string firstName =
(string)Request.QueryString.Get("TableName");

Just returns a null, even though in the part of the code
alert("about to post the data");
httpRequest.send(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
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...
10
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....
10
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...
5
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...
6
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....
2
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....
3
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...
0
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...
0
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.