如何修改在C#,ASP.NET的请求头 [英] How to modify request headers in c#,ASP .NET

查看:1953
本文介绍了如何修改在C#,ASP.NET的请求头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP .NET MVC项目和放大器;我必须改变HttpHeaders。看到foolowing code片断:

I am working on a ASP .NET mVC project & i have to change HttpHeaders. see the foolowing code snippet:

WebRequest req= HttpWebRequest.Create("myURL");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

req.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)");
req.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
req.Headers.Add("Accept-Language", "en-us,en;q=0.5");

这给出了一个例外,即

这头必须使用适当的属​​性进行修改\\ r \\ n参数名:。名称

This header must be modified using the appropriate property.\r\nParameter name: name.

任何suggets它的解决方案

Anyone suggets solution for it

推荐答案

在除了什么@Mehrdad Afshari说,你的 REQ 变量需要HttpWebRequest的类型的。 WebRequest类(抽象父类)不具备的UserAgent并接受属性。

In addition to what @Mehrdad Afshari says, your req variable needs to be of type HttpWebRequest. WebRequest (the abstract parent class) doesn't have the UserAgent and Accept properties.

HttpWebRequest req = WebRequest.Create( "http://..." ) as HttpWebRequest;

另外,我只是想确保你真的想改变您正在从你的MVC应用程序发送,而不是正从你的MVC应用程序发回的响应的请求头。你写了code的方式是有点混乱,你收到的反应,这将无法正常工作后,设置标头,因为你具体是指MVC。如果您在服务器上创建一个WebRequest的您是否正在使用的WebForms或MVC真的没关系,这个过程仍然是相同的。

Also, I just want to make sure that you are really trying to change the headers on a request that you are sending from your MVC application, not on the response that is being sent back from your MVC application. The way you've written the code is a little confusing as you are setting the headers after receiving the response, which won't work, and because you specifically refer to MVC. If you are creating a WebRequest on the server it really doesn't matter whether you are using WebForms or MVC, the process is still the same.

如果事实证明你正在试图改变响应的格式您发送回来,发表评论,让我知道。

If it turns out that you are trying to change the format of the response you are sending back, leave a comment and let me know.

这篇关于如何修改在C#,ASP.NET的请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆