gpt4 book ai didi

c# API 可以返回 HttpWebResponse

转载 作者:行者123 更新时间:2023-11-30 22:19:55 24 4
gpt4 key购买 nike

我想我想做的是使用 API 作为代理。它与 Fiddler 合作,并且有了这个想法。

我有一个网站,希望在 iframe 中显示另一个网站。但我需要删除“不打开”标题才能做到这一点。

所以计划是:从我的站点向 API 发送一个 url 字符串,API 向该 url 发出请求,获取响应,并且在不保存页面的情况下仅更改一些 header 并响应回我的网站。

问题:它不断返回一个 json。我试图将 html 代码作为字符串返回,但我的网站不会将其呈现到 iframe 中。

这是我的代码。

 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;
}
}

最佳答案

它是这样工作的

 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;
}
}

关于c# API 可以返回 HttpWebResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15300735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com