HTTPPOST - sending via ASP works, sending via ASPX does NOT work

MikeCombe
MikeCombe used Ask the Experts™
on
Using ASP I can send send a string using HTTPPOST & get an expected response.
Using ASPX I do NOT get an expected response.
It appears that the ASPX is NOT sending the string.

QUESTION: What am I doing wrong in the ASPX webpage

'-------------------------------------------------------
' test_httppost_send.asp   <--- works as expected
'-------------------------------------------------------
color = "red"
strMsg = "color=" & color
myURL = "http://www.someurl.com/test_httppost_receive.asp"
set xmlhttp = createobject("msxml2.serverxmlhttp")
xmlhttp.Open "POST", myURL, false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.Send (strMsg)
strResponse = xmlhttp.responseText
set xmlhttp = nothing
Response.Write strResponse
'-------------------------------------------------------




'-------------------------------------------------------
' test_httppost_send.aspx   <--- does NOT work as expected
'-------------------------------------------------------
dim color as string = "red"
dim strMsg as string = ""
dim myURL as string = "http://www.someurl.com/test_httppost_receive.asp"
dim myResponse as string = ""
strMsg = "color=" & color
dim xmlhttp = Server.CreateObject("msxml2.serverxmlhttp")
xmlhttp.Open ("POST", myURL, false)
xmlhttp.setRequestHeader ("Content-Type","application/x-www-form-urlencoded")
xmlhttp.Send (strMsg)
myResponse = xmlhttp.responseText
xmlhttp=nothing
Response.Write (myResponse)
'-------------------------------------------------------





'-------------------------------------------------------
' test_httppost_receive.asp <--- echo back the string posted
'-------------------------------------------------------
Sender_color = Request.Form("color")
Response.Write "Sender_color = " & Sender_color
'-------------------------------------------------------



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011
Commented:
Instead of using
dim xmlhttp = Server.CreateObject("msxml2.serverxmlhttp") in your test_httppost_send.aspx, please use code as provided

Dim strResult As String = String.Empty
        Dim color As String = "red"
        Dim strURL As String = "http://www.someurl.com/test_httppost_receive.asp"
        Dim objXML As WebRequest
        Dim strResponse As WebResponse
        Dim strMsg = "color=" & color

        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(strMsg)

        objXML = HttpWebRequest.Create(strURL)
        objXML.Method = "POST"
        objXML.ContentType = "application/x-www-form-urlencoded"
        objXML.ContentLength = byteArray.Length


        Dim dataStream As Stream = objXML.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

        Dim response As WebResponse = objXML.GetResponse()
        dataStream = response.GetResponseStream()

        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()

        Label1.Text = responseFromServer

        objXML = Nothing

Author

Commented:
I get this error:
The remote server returned an error: (500) Internal Server Error.
on this line:
Dim response As WebResponse = objXML.GetResponse()

Also, why would someone use the word "response" as an object?
That eliminates the use of Response.Write ("blah, blah")
Wouldn't a better object name be something like "WebResponse1" ?


Top Expert 2011

Commented:
Please use these imports on your aspx.vb page:

Imports System.Net
Imports System.IO

You could use naming convention that meets your requirement, code sample provided is to guide you in correct direction.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
already have these imports:

Imports System.Net
Imports System.IO

Top Expert 2011

Commented:
Could you please provide more details on error received on page.

Author

Commented:
Here's the error page:


Server Error in '/myPath' Application.
--------------------------------------------------------------------------------

The remote server returned an error: (500) Internal Server Error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Source Error:


Line 681:            dataStream.Close()
Line 682:
Line 683:            Dim WebResponse1 As WebResponse = objXML.GetResponse()
Line 684:            dataStream = WebResponse1.GetResponseStream()
Line 685:
 

Source File: myPath\myDoc.aspx    Line: 683

Stack Trace:


[WebException: The remote server returned an error: (500) Internal Server Error.]
   System.Net.HttpWebRequest.GetResponse() +5375997
   ASP.datatransfer_myPath_myDoc_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\path\myDoc.aspx:683
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3623; ASP.NET Version:2.0.50727.3618
Top Expert 2011

Commented:
Please see attached code, have tested and is working fine on test environment.

In the ZIP file attached rename "test_httppost_receive.asp.txt" to "test_httppost_receive.asp".


Also if the error still persists have a look at this link
http://forums.asp.net/t/1693667.aspx/1?The+remote+server+returned+an+error+500+Internal+Server+Error
HTTPRequest-.zip

Author

Commented:
The code in the accepted solution worked fine in the test environment. So, that's good. It is a great, clear example of making an HTTPPOST request using ASPX or VB.net.

The error that I referred to (08/08/11 10:05 AM, ID: 36330102) occured when I used that code in a production environment. The error: (500) Internal Server Error....is probably due to another issue.

Thanks for your help on providing a great ASPX example.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial