WCF service act as proxy for external web service

I'm working on developing proxy WCF service. So tasks that I have to achieve as part of this assignment are:

  1. I have to capture headers and content from an incoming request (from a web browser).
  2. Construct a web request for external web service with headers and content from incoming request.
  3. Execute web request.
  4. Capture headers/cookies/content form the Response of external web service.
  5. Construct response, add headers/cookies and send response back to browser.

I'm able to manage 1, 2 , 3 and 4 with help of web and stackoverflow. But not find any solution for task 5.

Questions:

  1. Response message from external web service is a json. How to send the same message in json format to web browser?
  2. Response from external web service has 'set-cookie' header. How to add this to web browser response?

I'm new to WCF and Web Service world. Using System.Net.Http.HttpClient to make a call to external web service. Open to change it to any other client library to achieve tasks 1 to 5.

Code blocks:

IService1.cs

    [OperationContract]
    [WebInvoke(Method = "POST", 
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "client/api")]
    string ProxyAPI(Stream inp);

Service1.cs

    public string ProxyAPI(Stream contentStream)
    {
        // Task 1: Capture Headers and content
        // Task 2: Construct web request with headers and content from incoming request
        HttpClient proxyClient = new HttpClient();
        Uri extWSuri = new Uri("http://router.sdc.com:8090/service/api");
        proxyClient.BaseAddress = extWSuri;

        // get input reqquest headers and add to httpclient onject
        WebOperationContext current = WebOperationContext.Current;
        WebHeaderCollection headers = current.IncomingRequest.Headers;
        string[] headerKeys = headers.AllKeys;
        foreach (string keyStr in headerKeys)
        {
            if (keyStr.ToLower().Equals("host"))
            {
                proxyClient.DefaultRequestHeaders.Add(keyStr, "router.simplifydc.com:8080");
            }
            else if (!keyStr.ToLower().StartsWith("cont"))
            {
                proxyClient.DefaultRequestHeaders.Add(keyStr, headers.GetValues(keyStr));
            }
        }

        // get input content data
        StreamReader reader = new StreamReader(contentStream);
        string contentData = reader.ReadToEnd();
        reader.Close();

        // create content for httpclient request
        StringContent contentRequest = new StringContent(contentData, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");

        // Task 3: execute web request
        Task<HttpResponseMessage> responseTask = proxyClient.PostAsync(extWSuri, contentRequest);
        responseTask.Wait();

        // Task 4: Capture headers/cookies/contnet from web response
        HttpResponseMessage response = responseTask.Result;
        HttpResponseHeaders resHeaders = response.Headers;
        Task<string> contentTask = response.Content.ReadAsStringAsync();
        contentTask.Wait();
        string responseMsg = contentTask.Result;

        // Task 5: construct response for incoming web browser request
        // ?????????????
        return responseMsg;
    }

Answers


If I get it right, the client application for your WCF service is a web browser. This means that it expects an HTML document as a response right? In this case, I would suggest to avoid using a WCF service.

Why don't you use an ASP.NET page which forms the HTML to return? In your ASP.NET code behind, you could execute the 4 four steps you describe. In your 5th step, you would adjust the HTML to contain the data you got in the previous steps from the external web service.

You could always construct the HTML response using the WCF service, but I cannot see why. You could use and HTML DOM parser (like this to construct the HTML response programmatically, but this would not be my first approach.

If your page requires pure data and not HTML, your WCF service could return data in JSON format. As you can see, JSON is a simple string which could be very easy to construct. It depends on the data you want to return, but it could be very easy implemented either manually, or using some specific library like Json.NET. Then your UI application would parse JSON and display data accordingly.

Hope I helped!


Need Your Help

Google Friend Connect ASP.NET

asp.net google-friend-connect

I am using the JS APIS for Google Friend Connect in my ASP.NET app. I am not able to run the application at all. If any body could please let me know how to get the FriendList and show it in ASP.NE...

image zoom, scroll view, crop

iphone ios image scrollbar

I simply zoom the picture by placing imageView on ScrollView. i want to crop the image portion, which is showed in imageView (after zooming) and save as a new image.