Solved

How to send an HTTPS POST to an external URL and receive response using Classic ASP

Posted on 2014-04-23
14
1,521 Views
Last Modified: 2014-05-07
Many years ago, I needed a way to send POST requests via HTTPS in order to interact with APIs such as Authorize.Net and UPS Online Tools, among others, from my Classic ASP pages.

I found a Windows server component (DLL) called KHTTP that would send out the requests and put the results into an ASP variable. It worked with HTTP and HTTPS, as well as regular POST and XML data, could pull in the content from any external URL (even if not POSTing data), and I have used it with a variety of APIs (and for other purposes) over the years. The component seems to be out-of-date at this point, although I still use it for many websites since it works well at what it does:

http://webscripts.softpedia.com/script/Networking-Tools/KHTTP-Component-30862.html

However, at the moment, I am trying to do HTTPS POST requests to one of the PayPal APIs. KHTTP does not seem to be working, because (I am told by PayPal tech support) it is likely the component is making my data urlencoded.

I'm a little confused by that, because I have used it successfully with many other APIs… notable ones too, such as the aforementioned Authorize.Net and UPS Online Tools. I don't know why their requirements for a POST request would be different from PayPal's.

I even setup my own listener, received my own POST, and the data seems to come through fine.

I don't fully understand the urlencoded issue, I thought that is something related to GET requests, not POST.

Using KHTTP, I don't have any option to send the data any other way. I simply assign a bunch of name/value pairs, I have the option to POST or GET, then put in the https URL, and the component does the rest, putting the response into a variable.

So if PayPal tech support is right, then a different component/utility function would be the next thing to try.

So, I need an alternative to KHTTP that will interface correctly with PayPal's API. Does this need to be a DLL that I install on the server? Or is there some other way to do this? It has been a long while since I have researched this kind of thing. All I really need to do is:

1. Assign name/value pairs.
2. POST to PayPal's API.
3. Receive the response back.

Would really appreciate any advice.
Thank you!
0
Comment
Question by:bbdesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 40018697
Below is a function I have for something similar.

The top portion creates the xml soap to be posted.  If you were to response.write the strRequest, you would get the xml.  

Where you see, Dim objXMLHTTP is the start of sending the post to the url

strResult is where the results (xml) is held and below that you can see how I am parsing the xml.

function searchTransactions(SalesOrderID)

strRequest =""
strRequest =strRequest&"<?xml version=""1.0"" encoding=""UTF-8""?>"
strRequest =strRequest&"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" "
strRequest =strRequest&"xmlns:ns1=""urn:usaepay"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" "
strRequest =strRequest&"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" "
strRequest =strRequest&"xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" "
strRequest =strRequest&"SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">"
strRequest =strRequest&"<SOAP-ENV:Body>"

strRequest =strRequest&"<ns1:searchTransactions> "

strRequest =strRequest&"<Token xsi:type=""ns1:ueSecurityToken"">"
strRequest =strRequest&"<ClientIP xsi:type=""xsd:string"">"& clientip &"</ClientIP>"
strRequest =strRequest&"<PinHash xsi:type=""ns1:ueHash"">"
strRequest =strRequest&"<HashValue xsi:type=""xsd:string"">"& hash &"</HashValue>"
strRequest =strRequest&"<Seed xsi:type=""xsd:string"">"& seed &"</Seed>"
strRequest =strRequest&"<Type xsi:type=""xsd:string"">"& hashtype &"</Type>"
strRequest =strRequest&"</PinHash>"
strRequest =strRequest&"<SourceKey xsi:type=""xsd:string"">"& sourcekey &"</SourceKey>"
strRequest =strRequest&"</Token>"

strRequest =strRequest&"<Search SOAP-ENC:arrayType=""ns1:SearchParam[1]"" xsi:type=""ns1:SearchParamArray""> "
strRequest =strRequest&"<item xsi:type=""ns1:SearchParam""> "
strRequest =strRequest&"<Field xsi:type=""xsd:string"">Invoice</Field> "
strRequest =strRequest&"<Type xsi:type=""xsd:string"">eq</Type> "
strRequest =strRequest&"<Value xsi:type=""xsd:string"">"&SalesOrderID&"</Value> "
strRequest =strRequest&"</item> "
strRequest =strRequest&"</Search> "
strRequest =strRequest&"<MatchAll xsi:type=""xsd:boolean"">true</MatchAll> "
strRequest =strRequest&"<Start xsi:type=""xsd:integer"">0</Start> "
strRequest =strRequest&"<Limit xsi:type=""xsd:integer"">10</Limit>" 
strRequest =strRequest&"<Sort xsi:type=""xsd:string"">created</Sort> "

strRequest =strRequest&"</ns1:searchTransactions> "

strRequest =strRequest&"</SOAP-ENV:Body> "
strRequest =strRequest&"</SOAP-ENV:Envelope>"



Dim objXMLHTTP
set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")

strURL = PostToUrl
objXMLHTTP.setTimeouts 10000, 60000 , 60000, 360000 
   objXMLHTTP.open "POST", "" & strURL & "" , False
   objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
   objXMLHTTP.setRequestHeader "Content-Length", Len(strRequest)
   objXMLHTTP.send(strRequest)

   strResult = objXMLHTTP.responseText

   
dim getCustPayMethText
   getCustPayMethText=strResult

 
    Set objReturn = CreateObject("MSXML2.DOMDocument.3.0")
    objReturn.loadXML objXMLHTTP.responseText

' ********** READ XML OUTPUT *********'


if not objReturn.getElementsByTagName("TransactionsMatched").Item(0) is Nothing  then
	TransactionsMatched=			objReturn.getElementsByTagName("TransactionsMatched").Item(0).text
	else
	TransactionsMatched="0"
end if
if cdbl(TransactionsMatched)>0 then
for x = 0 to cint(TransactionsMatched)-1

if not objReturn.getElementsByTagName("RefNum").Item(x) is Nothing  then
	RefNum=			objReturn.getElementsByTagName("RefNum").Item(x).text
	else
	RefNum="N/A"
end if

if not objReturn.getElementsByTagName("Result").Item(x) is Nothing  then
	Result=			objReturn.getElementsByTagName("Result").Item(x).text
	else
	Result="N/A"
end if

if not objReturn.getElementsByTagName("ResultCode").Item(x) is Nothing  then
	ResultCode=			objReturn.getElementsByTagName("ResultCode").Item(x).text
	else
	ResultCode="N/A"
end if

if not objReturn.getElementsByTagName("CardNumber").Item(x) is Nothing  then
	CardNumber=			objReturn.getElementsByTagName("CardNumber").Item(x).text
	else
	CardNumber="NA/A"
end if

if not objReturn.getElementsByTagName("TransactionType").Item(x) is Nothing  then
	TransactionType=			objReturn.getElementsByTagName("TransactionType").Item(x).text
	else
	TransactionType="0"
end if

if ResultCode = "A" then
	searchTransactions="Approved"
	else
	searchTransactions="Not Approved"
end if

end Function

Open in new window

0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40018715
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40018724
Looking at paypal you can see their soap header https://developer.paypal.com/docs/classic/api/PayPalSOAPAPIArchitecture/

<SOAP-ENV:Header>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
<Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
<Username>api_username</Username>
<Password>api_password</Password>
<Signature>api_signature</Signature>
<Subject>authorizing_account_emailaddress</Subject>
</Credentials>
</RequesterCredentials>
</SOAP-ENV:Header>

Open in new window


Where you see items like
<Username>api_username</Username>
that could be
<%

theXML="<Username>hard_coded_username</Username>"
' or
my_user_name="abc123"
theXML="<Username>"&my_user_name&"</Username>"

%>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bbdesign
ID: 40018759
You appear to be using XML in all of these cases. Isn't that different than regular POST "name value pairs"? In fact, the DLL I was using (KHTTP) had a switch that I had to use, to tell it I was posting XML data rather than "regular" data (which I assume is the same as what an HTML form would send, as opposed to XML)?

I'm not sure how to format non-XML data, if that is in fact what PayPal wants. That other answer you pasted above looks kind of promising too, so I will check that out.

Or maybe I am overcomplicating this, and its basically the same thing?

Thanks!
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40018806
The api from paypal I  looked up requires SOAP.  

For a direct post  http://support.microsoft.com/kb/290591
<%
	DataToSend = "id=1"
	dim xmlhttp 
	set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
	xmlhttp.Open "POST","http://somesite.com/Receiver.asp",false
	xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
	xmlhttp.send DataToSend
	Response.ContentType = "text/xml"
	Response.Write xmlhttp.responsexml.xml
        Set xmlhttp = nothing
%>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40018868
That's an interesting comment from the Paypal tech.  I'm using the Express Checkout API and everything that is posted is (and must be) urlencoded.  That is standard practice throughout the web.  Some simple values without any punctuation or special characters can be sent without urlencoding but everything else needs it.  I'm sure that if you looked at the data that Scott's code is sending you would see that it is definitely urlencoded.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40018879
Your link for KHTTP is no longer valid, it redirects to some other web site.
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40018889
ChillKat has this but you don't need it.  http://www.chilkatsoft.com/HttpActiveX.asp
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40018896
But I can't check KHTTP for this question if I can't download it.
0
 

Author Comment

by:bbdesign
ID: 40019065
I am using this for Payflow Gateway. Does that tell you anything, regarding SOAP and so forth?
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 40019092
Since the question here was about how to do an httppost, I think we have shown that. To prevent this question from getting off tangent, see if what we have shown makes sense.

As for integrating with paypal, I would suggest reading up and giving it a try.  We will need more details on your next question such as which exact api are you using, what code have you tried, what error are you getting.

There is payflow link https://www.paypal.com/webapps/mpp/referral/paypal-payflow-link and payflow pro https://www.paypal.com/webapps/mpp/referral/paypal-payflow-pro which is going to be more like what you used on authorize.net.  

Paypal pro has developer info https://developer.paypal.com/docs/classic/products/payflow-pro/  and you will see a list of pdf's.  One shows you can use either xml or name value pairs https://www.paypalobjects.com/webstatic/en_US/developer/docs/pdf/pp_payflowpro_guide.pdf   
TRXTYPE=S&TENDER=C&USER=SuperMerchant&PWD=SuperUserPassword&PARTNER=PayPal&
ACCT=5105105105105100&EXPDATE=1209&AMT=99.06&COMMENT1=Reservation&FIRSTNAME
=John&LASTNAME=Jones&STREET=123 Main St.&CITY=San 
Jose&STATE=CA&ZIP=123451234&BILLTOCOUNTRY=US&CVV2=123&CUSTIP=0.0.0.0 

Open in new window

Using this example http:#a40018806 you might be able to add that information in the "datatosend" variable.
0
 

Author Comment

by:bbdesign
ID: 40019678
Thank you so much, I will be checking into all of this later today.
0
 

Author Comment

by:bbdesign
ID: 40044667
In order to verify that I am sending the data correctly, I am trying to setup a listener to bounce back what it is receiving. Before I started messing around with XML, I was using regular FORM-style post data, and I wrote this simple ASP script:

for each item in request.form
            response.write(item & "=" & request.form(item) & chr(13) & chr(10))
next

How can I do something similar for my XML data so I can know for sure what I am sending?
0
 

Author Comment

by:bbdesign
ID: 40048070
Sorry for the long delay, I was able to get this to work using the sample code provided above. Thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question