我的C#代碼正在調用我的API服務(PHP),它需要將請求重定向到另一個url,並在返回之前處理響應。這裏是我的C#代碼:重定向到包含內容的另一個網址
HttpWebRequest request = null;
WebResponse response = null;
Stream writer = null;
request = (HttpWebRequest)WebRequest.Create(http://www.somewhere.com/proxy.php);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + this.builder.Boundry;
this.builder.RequestStream.Position = 0;
byte[] tempBuffer = new byte[this.builder.RequestStream.Length];
this.builder.RequestStream.Read(tempBuffer, 0, tempBuffer.Length);
writer = await request.GetRequestStreamAsync().ConfigureAwait(false);
writer.Write(tempBuffer, 0, tempBuffer.Length);
writer = null;
response = await request.GetResponseAsync().ConfigureAwait(false);
然後在proxy.php
:
<?php
// 1. Redirect everything (with the content stream from the C# code) to http://www.elsewhere.com
// 2. Process the response from http://www.elsewhere.com
// 3. Return processed data to my C# code
?>
我怎樣才能做到這一點?它需要我穿上這條線的請求流重定向:
writer = await request.GetRequestStreamAsync().ConfigureAwait(false);
writer.Write(tempBuffer, 0, tempBuffer.Length);
http://php.net/manual/en/function.header.php – mkaatman
爲何不從一開始就致電其他網站? –
由於'proxy.php'需要從其他網站獲取響應,並在將其返回到我的C#代碼之前進行更改。 – Darius