Link to home
Start Free TrialLog in
Avatar of MikeCombe
MikeCombeFlag for United States of America

asked on

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

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
'-------------------------------------------------------



ASKER CERTIFIED SOLUTION
Avatar of pateljitu
pateljitu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeCombe

ASKER

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" ?


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.
already have these imports:

Imports System.Net
Imports System.IO

Could you please provide more details on error received on page.
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
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
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.