By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

webRequest object and PayPal

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"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

Nov 19 '05 #2

P: n/a

"vMike" <Mi****************@noZorY.geZwaYrrenY.com> wrote in message
news:cp**********@ngspool-d02.news.aol.com...

"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


I forgot to mention. The code I provided is the a "listening page" that
paypal will post to. The way I test it is to do the following.
Create a page to post TO the listen page. Then create another page with the
following

<html>

<body>
VERIFIED
</body>
</html>

change the setting it the code I sent to so that the paypaly url is the page
with the Verified in it. Using your posting page, you can post a paypal
tranaction to the listening page. Good luck.

Mike
Nov 19 '05 #3

P: n/a
It works. Thanks!!

"vMike" wrote:

"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

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.