带有 GET 的 HttpWebRequest:401 未经授权

发布于 2024-11-27 18:51:44 字数 820 浏览 0 评论 0原文

我发现 httpWebRequest 有一些问题,我在其他论坛上阅读了所有相同的问题,但答案似乎不起作用。我的代码:

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp;
wr.ContentType = "text/html; charset=UTF-8"; 
wr.Method = "GET";
wr.Credentials = new NetworkCredential("user", "password");
resp = (HttpWebResponse)wr.GetResponse();

远程服务器返回错误:(401) 未经授权。 响应说 cookie 中没有身份验证令牌。我可以使用 POST 方法使用我的身份验证请求来接收此令牌。我什至尝试通过“new Cookie ("authToken",token_value)”将其放入CookieContainer。但结果是相同的 - 错误401。有人知道解决办法吗?

谢谢。

我使用 Zimbra Web 服务器,有权控制它。 .NET 4.0。我的 url 是我需要下载的 .eml 文件的路径。要指定文件,我需要添加一些 GET 参数:id 和部分。所以整个地址看起来像 http://someserver.info/service/content /get?id=1&part=1

I found some problem with httpWebRequest, I've read all the same issues on other forums, but answers don't seem to work. My code:

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp;
wr.ContentType = "text/html; charset=UTF-8"; 
wr.Method = "GET";
wr.Credentials = new NetworkCredential("user", "password");
resp = (HttpWebResponse)wr.GetResponse();

The remote server returned an error: (401) Unauthorized.
Response says there's no auth token in cookies. I can receieve this token using my auth request with POST method. I even tried to put it to CookieContainer by "new Cookie ("authToken",token_value)". But the result is the same - error 401. Does anybody know the solution?

Thanx.

I use Zimbra web server, have an access to control it. .NET 4.0. My url is the path to .eml file I need to download. To specify the file I need to add some GET parameters: id and part. So the whole address looks like http://someserver.info/service/content/get?id=1&part=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-12-04 18:51:44

(在OP的评论和问题编辑中回答。移至此处。请参阅问题没有答案,但问题在评论中解决(或在聊天中扩展)

OP 写道:

Zimbra 中的授权令牌称为 ZM_AUTH_TOKEN,因此您需要将 authtoken 放入 Cookie 中,如下所示:

wr.CookieContainer = new CookieContainer();
wr.CookieContainer.Add(new Uri(url), new Cookie("ZM_AUTH_TOKEN", rc.AuthToken));

您不需要添加身份验证标头,请求就会起作用

(Answered in the comments and question edits by OP. Moved here. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

The authorization token in Zimbra called ZM_AUTH_TOKEN so you need to put your authtoken in cookies like this:

wr.CookieContainer = new CookieContainer();
wr.CookieContainer.Add(new Uri(url), new Cookie("ZM_AUTH_TOKEN", rc.AuthToken));

You don't need to put the auth headers then, the request will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文