Tag: http headers

使用HttpWebRequest添加自定义标头

我不确定这些突出显示的值是什么类型的标头,但我应该如何使用HttpWebRequest添加它们? 突出显示的部分是否被视为http请求或标题数据的主体? 换句话说,哪种方式是正确的? 这是我目前使用的代码: HttpWebRequest request = (HttpWebRequest) WebRequest.Create(“/securecontrol/reset/passwordreset”); request.Headers.Add(“Authorization”, “Basic asdadsasdas8586”); request.ContentType = “application/x-www-form-urlencoded”; request.Host = “www.xxxxxxxxxx.com”; request.Method = “POST”; request.Proxy = null; request.Headers.Add(“&command=requestnewpassword”); request.Headers.Add(“&application=netconnect”); 但是,我应该使用以下代码来构建上面的Http请求吗? string reqString = “&command=requestnewpassword&application=netconnect”; byte[] requestData = Encoding.UTF8.GetBytes(reqString); HttpWebRequest request = (HttpWebRequest) WebRequest.Create(“/securecontrol/reset/passwordreset”); request.Headers.Add(“Authorization”, “Basic ashAHasd87asdHasdas”); request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = requestData.Length; request.Proxy = null; request.Host = “www.xxxxxxxxxx.com”; […]

HttpClient检索所有标头

目前,我正在研究API包装器。 如果我发送一个错误的Consumer Key ,服务器将在标题中返回Status为403 Forbidden 。 它还将传递自定义标头。 如何实际检索这些自定义标头? 这是从服务器接收的响应。 Cache-Control: private Date: Wed, 01 May 2013 14:36:17 GMT P3P: policyref=”/w3c/p3p.xml”, CP=”ALL CURa ADMa DEVa OUR IND UNI COM NAV INT STA PRE” Server: Apache/2.2.23 (Amazon) Status: 403 Forbidden X-Error: Invalid consumer key. X-Error-Code: 152 X-Powered-By: PHP/5.3.20 Connection: keep-alive 我需要检索X-Error和X-Error-Code 。 目前,我正在使用HttpClient类来处理请求。 如果我在VS Studio 2012的Quick Watch下看到标题响应,我可以像这样找到它 […]

使用RestSharp设置’Content-Type’标头

我正在为RSS阅读服务建立一个客户端。 我正在使用RestSharp库与他们的API进行交互。 API声明: 创建或更新记录时,必须设置application / json; charset = utf-8作为Content-Type标头。 这就是我的代码: RestRequest request = new RestRequest(“/v2/starred_entries.json”, Method.POST); request.AddHeader(“Content-Type”, “application/json; charset=utf-8”); request.RequestFormat = DataFormat.Json; request.AddParameter(“starred_entries”, id); //Pass the request to the RestSharp client Messagebox.Show(rest.ExecuteAsPost(request, “POST”).Content); 然而; 服务返回“错误415:请使用’Content-Type:application / json; charset = utf-8’标题。” 为什么RestSharp不通过标题?

缓存控制:无存储,必须重新validation未发送到IIS7 + ASP.NET MVC中的客户端浏览器

我正在尝试确保某个页面永远不会被缓存,并且在用户单击后退按钮时从不显示。 这个非常高评价的答案(目前1068票)表示使用 : Response.AppendHeader(“Cache-Control”, “no-cache, no-store, must-revalidate”); Response.AppendHeader(“Pragma”, “no-cache”); Response.AppendHeader(“Expires”, “0”); 但是在IIS7 / ASP.NET MVC中,当我发送这些头时,客户端会看到这些响应头: Cache-control: private, s-maxage=0 // that’s not what I set them to Pragma: no-cache Expires: 0 缓存控制头怎么了? IIS7或ASP.NET本机的内容是否会覆盖它? 我检查了我的解决方案,我没有覆盖此标头的代码。 当我添加Response.Headers.Remove(“Cache-Control”); 首先,它没有区别: Response.Headers.Remove(“Cache-Control”); Response.AppendHeader(“Cache-Control”, “no-cache, no-store, must-revalidate”); Response.AppendHeader(“Pragma”, “no-cache”); Response.AppendHeader(“Expires”, “0”); 当我添加[OutputCache]属性时: [OutputCache(Location = OutputCacheLocation.None)] public ActionResult DoSomething() { Response.Headers.Remove(“Cache-Control”); Response.AppendHeader(“Cache-Control”, “no-cache, […]

获得Request.Headers值

非常简单我敢肯定,但是把我拉到墙上! 我在我的Web应用程序中使用了一个组件,通过添加标题“XYZComponent = true”在Web请求中标识自己 – 我遇到的问题是,您如何在视图中检查此问题? 以下不会工作: if (Request.Headers[“XYZComponent”].Count() > 0) 这不是: if (Request.Headers.AllKeys.Where(k => k == “XYZComponent”).Count() > 0) 如果尚未设置标头变量,则两者都抛出exception。 非常感激任何的帮助。

C# – 连接:在HttpWebRequest期间未发送保持活动标头

我正在尝试发送以使用我的HttpWebRequest发送以下标头: Connection: keep-alive 但是,标头永远不会发送。 Fiddler2显示,每当我在Google Chrome中请求该页面时,都会发送标题。 但是,我的应用程序由于某种原因拒绝发送此标头。 我已将KeepAlive属性设置为true (默认情况下默认为true ),但标题仍未发送。 我正在尝试使用多个HttpWebRequests发送此标头,但它们基本上都是这样的: HttpWebRequest logIn6 = (HttpWebRequest)WebRequest.Create(new Uri(responseFromLogIn5)); logIn6.CookieContainer = cookies; logIn6.KeepAlive = true; logIn6.Referer = “https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/”; logIn6.UserAgent = “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1”; logIn6.Accept = “text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”; logIn6.Headers.Add(“Accept-Encoding:gzip,deflate,sdch”); logIn6.Headers.Add(“Accept-Language:en-US,en;q=0.8”); logIn6.Headers.Add(“Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3”); logIn6.AllowAutoRedirect = false; HttpWebResponse logIn6Response = (HttpWebResponse)logIn6.GetResponse(); string responseFromLogIn6 = logIn6Response.GetResponseHeader(“Location”); […]

Content-Disposition标头中的Unicode

我正在使用在HttpHandler子项中实现的HttpContext对象来下载文件,当我在文件名中有非ascii字符时,它在IE中看起来很奇怪,而在Firefox中看起来很好。 以下是代码: – context.Response.ContentType = “.cs”; context.Response.AppendHeader(“Content-Length”, data.Length.ToString()); context.Response.AppendHeader(“Content-Disposition”, String.Format(“attachment; filename={0}”,filename)); context.Response.OutputStream.Write(data, 0, data.Length); context.Response.Flush(); 当我在文件名字段中提供”””””””””””””””””””””””””””””””””””””””””””””名字在firefox中看起来很好。 添加EncodingType和charset一直没用。 即它是’ß””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””’ Ô””””””””””””””””””””’ 任何想法如何解决这个问题?

带有自定义标头的MVC重定向

希望对于那里的某个人来说,这是一个简单的问题。 基本上在收到我的MVC控制器的请求后,我想: 在响应中添加“授权”标头 重定向到另一个域上的另一个应用程序 阅读此外部站点的“授权”标题。 它似乎是重定向的行为,剥离了我的所有自定义标题和重定向。 我的问题是,我如何添加新的标头,并执行重定向,并在接收主机[在重定向结束时]的标题中显示该标题?

HttpModule为请求添加标头

这看似简单的操作。 我们的开发环境(在XP / IIS 5上运行)需要在每个到达我们应用程序的HttpRequest中添加一些头文件。 (这是为了模拟我们在开发中没有的生产环境)。 乍一看,这看起来像一个简单的HttpModule,沿着这样的方向: public class Dev_Sim: IHttpModule { public void Init(HttpApplication app) { app.BeginRequest += delegate { app.Context.Request.Headers.Add(“UserName”, “XYZZY”); }; } public void Dispose(){} } 但是在尝试这样做时,我发现Request的Headers集合是只读的,Add方法失败并带有OperationNotSupportedexception。 花几个小时在谷歌上研究这个问题,我找不到一个应该是一个相对直接的问题的简单答案。 有没有人有任何指针?

HttpClient请求像浏览器

当我通过HttpClient类调用网站www.livescore.com时,我总是得到错误“500”。 可能是来自HttpClients的服务器阻止请求。 1)还有其他方法可以从网页上获取HTML吗? 2)如何设置标题以获取HTML内容? 当我在浏览器中设置标题时,我总是得到stange编码的内容。 http_client.DefaultRequestHeaders.TryAddWithoutValidation(“Accept”, “text/html,application/xhtml+xml,application/xml”); http_client.DefaultRequestHeaders.TryAddWithoutValidation(“Accept-Encoding”, “gzip, deflate”); http_client.DefaultRequestHeaders.TryAddWithoutValidation(“User-Agent”, “Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0”); http_client.DefaultRequestHeaders.TryAddWithoutValidation(“Accept-Charset”, “ISO-8859-1”); 3)我如何解决这个问题? 有什么建议? 我在C#和HttpClientClass中使用Windows 8 Metro Style App