doing a POST to .ASP from ASP.NET page

  • Thread starter Thread starter John Edwards
  • Start date Start date
J

John Edwards

Hello,
What is the best way to do a POST from an ASP.NET page to
an old time ASP page. The ASP accesses the data through
Request.Form("username") method. Looks like a normal
WebRequest with POST method is not able to see the input
fields in the form of the posting ASP.NET page. I would
like to post something to the ASP page and get the
response back to the ASP.NET. Request.QueryString() is
working fine, only accessing the form contents have the
problem.

Thanks for any help

Regards,
John
 
John,

How are you setting the values for the post when you are making the call
through an HttpWebRequest?
 
Thanks for the reply. My ASP page looks something like the following

//myprocessor.asp
<%@ LANGUAGE="VBScript" %>

<%
strMemberGroup=Request.Form("MemberGroup")
s1 = "strMemberGroup=" & strMemberGroup & "<BR>"
Response.Write(s1)
Response.Write("done")
%>

Now my ASPX page goes something like the following. As you can see, there is
an input field MemberGroup in ASPX. And ASP is not able to get the value of
the input filed.

<form id="FormDomainMembers" name="FormDomainMembers" method="post"
runat="server">
<TABLE id="Table1">
</TBODY>
<TR>
<TD><input id="MemberGroup" type="hidden" name="MemberGroup"
runat="server"></TD>
<TD</TD>
<TD>
<asp:linkbutton id="LinkButtonAddMembers" runat="server">
submit request
</asp:linkbutton>
</TD>
</TR>
</TBODY>
</TABLE>
</form>

private void LinkButtonAddMembers_Click(object sender, System.EventArgs e)
{
string sURL = "http://localhost/myweb/myprocessor.asp";
WebRequest request = WebRequest.Create (sURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
writeToURL (request, sURL);
string htmlContent = retrieveFromURL (request);
Response.Write (htmlContent );
}

void writeToURL (WebRequest request, string data)
{
byte [] bytes = null;
bytes = System.Text.Encoding.ASCII.GetBytes (data);
request.ContentLength = bytes.Length;
Stream outputStream = request.GetRequestStream ();
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Close ();
}

String retrieveFromURL (WebRequest request)
{
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader (responseStream);
return reader.ReadToEnd ();
}
 
John said:
Thanks for the reply. My ASP page looks something like the following

//myprocessor.asp
<%@ LANGUAGE="VBScript" %>

<%
strMemberGroup=Request.Form("MemberGroup")
s1 = "strMemberGroup=" & strMemberGroup & "<BR>"
Response.Write(s1)
Response.Write("done")
%>

Now my ASPX page goes something like the following. As you can see, there is
an input field MemberGroup in ASPX. And ASP is not able to get the value of
the input filed.

<form id="FormDomainMembers" name="FormDomainMembers" method="post"
runat="server">
<TABLE id="Table1">
</TBODY>
<TR>
<TD><input id="MemberGroup" type="hidden" name="MemberGroup"
runat="server"></TD>
<TD</TD>
<TD>
<asp:linkbutton id="LinkButtonAddMembers" runat="server">
submit request
</asp:linkbutton>
</TD>
</TR>
</TBODY>
</TABLE>
</form>

private void LinkButtonAddMembers_Click(object sender, System.EventArgs e)
{
string sURL = "http://localhost/myweb/myprocessor.asp";
WebRequest request = WebRequest.Create (sURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
writeToURL (request, sURL);
string htmlContent = retrieveFromURL (request);
Response.Write (htmlContent );
}

void writeToURL (WebRequest request, string data)

So for data you pass in sURL which is
"http://localhost/myweb/myprocessor.asp".
{
byte [] bytes = null;
bytes = System.Text.Encoding.ASCII.GetBytes (data);
request.ContentLength = bytes.Length;
Stream outputStream = request.GetRequestStream ();

Then you send that URL as the request body. That doesn't help, you need
to send the form data (Request.Form) in the request body and not the URL.
I would guess if you just read the ASP.NET Request.InputStream and write
it to WebRequest request.GetRequestStream() then it will work fine. But
I haven't tested that.
 
Martin said:
John said:
Thanks for the reply. My ASP page looks something like the following

//myprocessor.asp
<%@ LANGUAGE="VBScript" %>

<%
strMemberGroup=Request.Form("MemberGroup")
s1 = "strMemberGroup=" & strMemberGroup & "<BR>"
Response.Write(s1)
Response.Write("done")
%>

Now my ASPX page goes something like the following. As you can see,
there is
an input field MemberGroup in ASPX. And ASP is not able to get the
value of
the input filed.

<form id="FormDomainMembers" name="FormDomainMembers" method="post"
runat="server">
<TABLE id="Table1">
</TBODY>
<TR>
<TD><input id="MemberGroup" type="hidden" name="MemberGroup"
runat="server"></TD>
<TD</TD>
<TD>
<asp:linkbutton id="LinkButtonAddMembers" runat="server">
submit request
</asp:linkbutton>
</TD>
</TR>
</TBODY>
</TABLE>
</form>

private void LinkButtonAddMembers_Click(object sender,
System.EventArgs e)
{
string sURL = "http://localhost/myweb/myprocessor.asp";
WebRequest request = WebRequest.Create (sURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
writeToURL (request, sURL);
string htmlContent = retrieveFromURL (request);
Response.Write (htmlContent );
}

void writeToURL (WebRequest request, string data)


So for data you pass in sURL which is
"http://localhost/myweb/myprocessor.asp".
{
byte [] bytes = null;
bytes = System.Text.Encoding.ASCII.GetBytes (data);
request.ContentLength = bytes.Length;
Stream outputStream = request.GetRequestStream ();


Then you send that URL as the request body. That doesn't help, you need
to send the form data (Request.Form) in the request body and not the URL.
I would guess if you just read the ASP.NET Request.InputStream and write
it to WebRequest request.GetRequestStream() then it will work fine. But
I haven't tested that.

I wrote a quick test as follows

string SendPostData () {
string URL = @"http://localhost/javascript/test20040109.asp";
HttpWebRequest httpRequest = (HttpWebRequest) WebRequest.Create(URL);
httpRequest.Method = "POST";
httpRequest.ContentLength = Request.ContentLength;
httpRequest.ContentType = Request.ContentType;
Stream httpRequestStream = httpRequest.GetRequestStream();
byte[] b = new byte[512];
int readBytes;
while ((readBytes = Request.InputStream.Read(b, 0, b.Length)) != 0) {
httpRequestStream.Write(b, 0, readBytes);
}
httpRequestStream.Close();
StreamReader streamReader = new
StreamReader(httpRequest.GetResponse().GetResponseStream());
string response = streamReader.ReadToEnd();
streamReader.Close();
return response;
}

and that works for me.
 
Thanks Martin, that just does the job! Can I trouble you with one more
related issue?

The input box is a server type control, and I want to update the content of
the input box right before calling the SendPostData function you provided.
In this case, the ASP page gets the updated value from the input box only
during the second call. As you can see, the the first time ASP is called, it
gets the default value specified for the input box at design time. How can I
take care of that?

Regards,
John
 
John said:
The input box is a server type control, and I want to update the content of
the input box right before calling the SendPostData function you provided.
In this case, the ASP page gets the updated value from the input box only
during the second call. As you can see, the the first time ASP is called, it
gets the default value specified for the input box at design time. How can I
take care of that?

I am not sure I understand what you are doing. The first time your
ASP.NET page is called is probably with a HTTP GET request in which case
no data is POSTed at all and then it doesn't make sense to try to write
any request body to an ASP page.
In general that SendPostData function writes the exact request body the
ASP.NET page received to the ASP page thus if you want to receive data
with ASP.NET page but then change some values before you pass the data
on to the ASP page then the function doesn't help, you would better read
out values from Request.Form and build your own new request body for ASP.
But I am guessing, it is probably better if you post the relevant
snippets from your code. And you might want to use the
dotnet.framework.aspnet for ASP.NET questions.
 
Back
Top