My C# code is calling my API service (PHP), which needs to redirect the request to another url, and process the response before returning it. Here is my C# code:
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);
And then in 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
?>
How can I do this? It needs to redirect the request stream that I put on this line:
writer = await request.GetRequestStreamAsync().ConfigureAwait(false);
writer.Write(tempBuffer, 0, tempBuffer.Length);