Joshua Pack
Joshua Pack

Reputation: 910

Sending Post Data through a HTTP Request POST

I am trying to post data from a Textarea to a classic ASP script that updates the MS SQL on the local machine then posts to a PHP script on another server. However doing the below does not work, as it will cut off the data for the textarea. It has special characters in it for HTML and other data.

So my question is, how do I pass this data through a POST using ASP Classic?

I have been searching all day, so hopefully someone can enlighten me, thanks!

    strUrl = "http://www.example.com/index.php"
    requestData = request("request")
    requestData2 = request("request2")

    strData = "request=" & requestData & "&request2=" & requestData2

    postHTML (strUrl, strData)

function postHTML (strUrl, strData)
    Set xmlHttp = Server.Createobject("MSXML2.ServerXMLHTTP")
    xmlHttp.Open "POST", strUrl, False
    xmlHttp.setRequestHeader "User-Agent", "asp httprequest"
    xmlHttp.setRequestHeader "content-type", "application/x-www-form-urlencoded"
    xmlHttp.Send strData
    postHTML = xmlHttp.responseText
    xmlHttp.abort()
    set xmlHttp = Nothing   
end function

For example if an & is in the requestdata2, anything after that & is truncated, because it is thinking it is starting a new string. But I want to be able to pass this along.

Only Solution I can think of as of right now: do a string replace for special characters such as = and & and restore them with another string replace on the php server. This however is not what I want to accomplish this task. I would like the correct way of sending a post.

Upvotes: 3

Views: 10794

Answers (1)

johna
johna

Reputation: 10772

You need to URL encode the parameter values, so change this line...

strData = "request=" & requestData & "&request2=" & requestData2 

...to...

strData = "request=" & Server.UrlEncode(requestData) & "&request2=" & Server.UrlEncode(requestData2)

The receiving server should automatically decode the values.

Upvotes: 2

Related Questions