完成POST并重定向到第三方站点

时间:2009-11-18 16:03:32

标签: c# forms post

我一直在为此奋斗三天,我想向第三方提供商做一个手动帖子,然后从网络用户那里获取详细信息(在这种情况下,提供商是PayFast),然后将重定向回到我的网站取决于成功与否。

我已尝试在此网站上使用大量示例,其他包括手动重新创建表单的示例,如(http://www.jigar.net/articles/viewhtmlcontent78.aspx所示)我发现的主要示例类似于stackoverflow上的帖子问题1167067

这是我的代码中的结果,它创建流的接缝,但我无法弄清楚如何将控件发送到提供者网站,因为response.redirect杀死了流

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 buffer (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

1 个答案:

答案 0 :(得分:0)

解决了它,感谢http://www.jigar.net/articles/viewhtmlcontent78.aspx

我根据文档创建了一个新类,并做了一些调整,比如初始化URL作为声明的一部分,它就像一个魅力,感谢JigJar

我之前找到了这个解决方案,并放弃了它。