How to forward a REST web service using C# (WCF)?
There is a web service that can only be consumed via http and a client that can only consume https web services. Therefore I need some intermediary that forwards on requests received in https and returns responses in http.
Supposing that the intermediary is completely dumb save for the fact that it knows where the web service endpoint is (i.e. it doesn't know what the signature of the service is, it just knows that it can communicate with it via an http web request, and it listens on some https uri, forwarding on anything it receives), what is the most simple way of ach开发者_StackOverflowieving this?
I've been playing around with this all day and am not sure how to achieve the "dumb" bit, i.e. not knowing the signature for passing back the verbatim response.
A dumb intermediary is essentially a proxy. Your best bet might to be just use standard asp.net pages (instead of shoehorning into service functionality like ASMX or WCF which are just going to fight you) so you can receive the request exactly as-is and process it in a simple way using standard request/response. You can make use of HttpWebRequest
class to forward the request on to the other endpoint.
- Client requests https://myserver.com/forwarder.aspx?forwardUrl=http://3rdparty.com/api/login
- myserver.com (your proxy) reads querystring
forwardUrl
and any POST or GET request included. - myserver.com requests to http://3rdparty.com/api/login and passes along GET or POST data sent from the client.
- myserver.com takes response and sends back as response to other endpoint (essentially just
Response.Write
contents out to the response)
You would need to write forwarder.aspx to process the requests. Code for forwarder.aspx
would be something like this (untested):
protected void Page_Load(object sender, EventArgs e)
{
var forwardUrl = Request.QueryString["forwardUrl"];
var post = new StreamReader(Request.InputStream).ReadToEnd();
var req = (HttpWebRequest) HttpWebRequest.Create(forwardUrl);
new StreamWriter(req.GetRequestStream()).Write(post);
var resp = (HttpWebResponse)req.GetResponse();
var result = new StreamReader(resp.GetResponseStream).ReadToEnd();
Response.Write(result); // send result back to caller
}
精彩评论