0

假设我有两个网站,网站-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。

4

1 回答 1

0

我在另一个网站上回答了类似的问题。为了您的方便,这里有一个示例代码:

    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();

可以在此处找到完成相同功能的替代方法

于 2013-09-19T08:50:00.887 回答