WebRequest 不会传递标头 [英] WebRequest will not pass headers

查看:20
本文介绍了WebRequest 不会传递标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有方法,我之前已经做了 100 次没有问题,但现在我被难住了.

i have trolled and tried everything, i have done this 100's of times before without issues but now im stumped.

我只是想从网络请求中传递标头信息.

I simply want to pass header info from a webrequest.

客户端单独运行并连接到 wcf 休息服务.该服务具有以下代码,该代码被点击:

The client runs separately and connects to a wcf rest service. The service has this code which gets hit:

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");

客户有这个:

    var client = WebRequest.Create(url);
    client.Method = type.ToString().ToUpper();
    client.ContentType = "application/json";
    client.ContentLength = bytes.Length;
    client.Headers.Add(String.Format("Authorization: {0}", "test12345"));

我也试过例如:

    client.Headers["X-Requested-With"] = "test1234";
    client.Headers["Authorization"] = "test1234";

无论我如何尝试设置标题,它们都不会被传递.当我查看提琴手中的请求时,授权永远不会存在.我也尝试过发送 X-Requested-With 等......但无论标题如何都不会通过.

No matter how i try and set the headers they are never passed. When I look at the request in fiddler, Authorization is NEVER there. I have also tried to send X-Requested-With, etc... but no matter what the header will not pass.

提前致谢.

更新:我找到了问题,但没有找到解决方案:在 ASP.NET MVC 控制器中使用时,WebRequest 不会传递标头

UPDATE: I found the issue, but not the solutions: WebRequest will not pass headers when used from within ASP.NET MVC controller

推荐答案

Use DefaultRequestHeaders

Use DefaultRequestHeaders

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("test12345")

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("test12345")

这篇关于WebRequest 不会传递标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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