开发者

Complete POST and redirect to a third party site

I have been struggling with this for three days, I want to do a manual post to a third party provider, that will then take details from the web user (in this case the provider is PayFast) and then will redirect back to my site depending on the success or failure.

I have tried using a number of examples on this site and others including one that manually recreates the form as can be seen at (http://www.jigar.net/articles/viewhtmlcontent78.aspx) the main example that I find is similar to the post at stackoverflow questions 1167067

here is the result in my code, which seams to create the stream, but I cannot figure out how to send the control to the providers website since response.redirect kills the stream

string vystup = null;
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes(pPostData);
//Initialisation, we use localhost, change if appliable
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(PF_HOST);
//Our method is post, otherwise the buf开发者_如何学编程fer (postvars) would be useless
WebReq.Method = "POST";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
PostResult = "=Status Code: " + WebResp.StatusCode ;
Console.WriteLine(WebResp.Server);

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
vystup = _Answer.ReadToEnd();

PostResult = "redirect called...";

// Need to now send the post form to PayFast


Solved it,thanks to http://www.jigar.net/articles/viewhtmlcontent78.aspx

I created a new class as per the doc, and made a few tweaks, like initializing th URL as part of the declaration and it works like a charm, thanks to JigJar

I had found this solution before, and given up on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜