假设我有两个网站,网站-A和网站-B。
网站-A收到一个帖子请求(来自第三方网站),我想将此请求及其数据转发给网站-B。我尝试了以下方法,但它似乎没有导航到网站B.
string newUrl = "http://localhost/WebSite-B/Test.aspx";
HttpRequest original = HttpContext.Current.Request;
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(newUrl);
newRequest.ContentType = original.ContentType;
newRequest.Method = original.HttpMethod;
newRequest.UserAgent = original.UserAgent;
byte[] originalStream;
using (var memoryStream = new MemoryStream())
{
original.InputStream.CopyTo(memoryStream);
originalStream = memoryStream.ToArray();
}
Stream reqStream = newRequest.GetRequestStream();
reqStream.Write(originalStream, 0, originalStream.Length);
reqStream.Close();
newRequest.CookieContainer = new CookieContainer();
newRequest.GetResponse();
GetResponse()似乎没有打开或导航到请求的页面。
请帮助我将网站A收到的POST请求传递给网站-B。
答案 0 :(得分:0)
我在另一个网站site中回答了类似的问题。为方便起见,这里有一个示例代码:
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "now=" + strId;
postData += ("&random=" + random);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(http://www.domain.com/controler/action);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
可以找到实现相同功能的替代方法here