"Stephane" <St******@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
Hi,
I'm trying to use PayPal and its Instant Payment Notification. In short,
when a payment is made, PayPal send a post to my server and I post it back
to PayPal.
I'm using WebRequest to do this. I receive the PayPal post, but I can't
post it back. It's always giving me a Time out.
Here's my code:
// Setup Web request
objRequest = System.Net.WebRequest.Create(url);
objRequest.Timeout = timeoutSeconds * 1000;
objRequest.ContentType="application/x-www-form-urlencoded";
objRequest.Method = "POST";
// Create a copy of the http post received
// to post it back to paypal
NameValueCollection form = new NameValueCollection(Request.Form);
string hdrs = "";
foreach ( string var in form )
{
if (form[var] != null)
objRequest.Headers.Add(var,form[var]);
}
// Adds the Paypal validation field to the request
objRequest.Headers.Add("cmd","_notify-validate");
// Retrieve data from request
objResponse = objRequest.GetResponse();
...Then, nothing happens here and I receive a timeout webException.
I'm using this in other class where it's working really well. Also, I made
a HTML form to test the post from my server and it's working too. The PayPal
url exists and I don't understand why I can't reach it via webRequest.
Any idea?
Thanks,
Stephane
When paypal post to your post page you only need to send back the date they
posted with the appended cmd info
Try something like this. Careful of screen wrap and you will need to add
your code to handle the response you get
I have use setting in my web config. you will have to replace those values
with your own values.
Dim stringPost, stringResult As String
Sub Page_Load(sender as object, e as EventArgs)
Dim mgWebRequest As HttpWebRequest
Dim mgWebResponse As HttpWebResponse
Dim mgStreamWriter As StreamWriter
Dim mgStreamReader As StreamReader
stringPost = Request.Form.tostring()
' Send Data to paypal with append
mgWebRequest
=CType(WebRequest.Create(ConfigurationSettings.App Settings("paypalURL")),Htt
pWebRequest)
if request.form.get("receiver_email") =
ConfigurationSettings.AppSettings("paypalrec") then
mgWebRequest.Method = "POST"
mgWebRequest.ContentLength = stringPost.Length + 21 'length plus
21 because &cmd=_notify-validate is 21 chars long
mgWebRequest.ContentType = "application/x-www-form-urlencoded"
mgStreamWriter = Nothing
mgStreamWriter = New StreamWriter(mgWebRequest.GetRequestStream())
stringPost = stringPost + "&cmd=_notify-validate"
mgStreamWriter.Write(stringPost)
mgStreamWriter.Close()
mgWebResponse = CType(mgWebRequest.GetResponse(),HttpWebResponse)
mgStreamReader = New StreamReader(mgWebResponse.GetResponseStream())
stringResult = mgStreamReader.ReadToEnd()
mgStreamReader.Close()
if instr(1,stringResult,"VERIFIED") > 0 then
if request.form.get("payment_status") = "Completed" then
'code to write to data base
else
'code to handle other responses
end if
else if instr(1,stringResult, "INVALID") > 0 then
'code to handle the INVALID response
else
'code to handle unknown response
End if
else
'code to handle unrequest paypal posts. do really need anything here
end if
end sub