3

what I am trying to do -I guess- is use an API as a proxy. It worked with Fiddler and there was the idea.

I have a website and wish to show another into an iframe. But i need to remove the 'no-open' header to do that.

So the plan is: Sent a url string to the API from my site, the API makes a request to that url, gets the response, and without saving the page just changes a few headers and respond back to my website.

Problem: It keeps returning a json. I tried to return the html code as a string, but then my website will not render it into iframe.

This is my code.

 public class ProductsController : ApiController
{
    public HttpWebResponse GetProductsByCategory(string url, int edit)
    {            
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Http.Get;
        request.ContentType = "text/html";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //
        if (edit == 1)
        {                              
            response.Headers.Set("Content-Disposition", "");
            response.Headers.Set("X-Download-Options", "");               
        }
        response.Headers.Set("Content-Type", "text/html");
        response.Headers.Set("APIflag", "Hi!");

        Stream theHTML = response.GetResponseStream();
        StreamReader objReader = new StreamReader(theHTML);
        string myHTML = objReader.ReadToEnd();

        System.Diagnostics.Debug.WriteLine("url was: "+url); //is OK
        System.Diagnostics.Debug.WriteLine("edit flag was: " +edit); //is OK
        System.Diagnostics.Debug.WriteLine(myHTML); //is OK

        return response;
    }
}

2 Answers 2

3

This way it worked

 public class ProductsController : ApiController
{      
    public HttpResponseMessage GetProductsByCategory(string url)
    {          
        HttpResponseMessage theResponse = null;
        // init a wep api Client
        var myClient = new HttpClient();
        var theTask = myClient.GetAsync(url).ContinueWith((lamdaObj) =>
        {
            theResponse = lamdaObj.Result;
        });
        // wait for task to complete. Not really async, is it?!
        theTask.Wait();
        // remove annoying header 
        theResponse.Content.Headers.Remove("Content-Disposition");
        return theResponse;
    }
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

Much easier than what I was doing and include the "annoying header" as you put it, that I needed. Thank you!
0

You may want to look at the ASP.net web api. You could do this with the WCF HTTP stack located at http://wcf.codeplex.com/. It has been included in the ASP.net web api. http://www.asp.net/web-api

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.