.NET CF 2.0 HttpWebRequest的pre-认证和第一个请求发送凭据 [英] .Net CF 2.0 HttpWebRequest pre-authentication and sending credentials on first request

查看:300
本文介绍了.NET CF 2.0 HttpWebRequest的pre-认证和第一个请求发送凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用自己的修改基本身份验证的版本bit.ly的REST API进行通信。不过,为了这个工作的HttpWebRequest需要附加在第一次请求的凭据,但是,HttpWebRequest的不会在第一次发送请求证书,他们将等待401发送任何凭据,即使preAuthenticate设置为true之前(以preAuthenticate它会发送所有后续请求凭据)。

I'm attempting to communicate with bit.ly's REST API using their modified version of basic authentication. However in order for this to work HttpWebRequest needs to attach the credentials on the first request, however, HttpWebRequest will not send credentials on the first request and will wait for a 401 before sending any credentials even if PreAuthenticate is set to true (with PreAuthenticate it will send credentials for all subsequent requests).

我已尝试下面得到的HttpWebRequest上班的路上bit.ly需要它:

I have attempted the following to get HttpWebRequest to work the way bit.ly requires it:

(1)发送格式 HTTP请求://用户名:password@api.bi .ly /法

不受bit.ly支持(因为这是一个假的实现基本身份验证,他们只检​​查标头)。

Not supported by bit.ly (as this is a fake implementation of basic authentication they only check the header).

(2)手动注入授权头入HttpWebRequest的。

(2) Manually inject the "Authorization" header into the HttpWebRequest.

在净CF不可能

(3)继承HttpWebRequest的WebRequest的,或在其他类来实现所需的行为。

(3) Inherit HttpWebRequest or WebRequest in another class as to implement the required behavior.

不可能的,因为HttpWebRequest类被注册为HTTP服务请求和HTTPS默认方案。尝试注册一个不同的WebRequest子代这些计划会重复prefixes不允许失败。

Not possible as The HttpWebRequest class is registered to service requests for HTTP and HTTPS schemes by default. Attempts to register a different WebRequest descendant for these schemes will fail as duplicate prefixes are not allowed.

因此​​,任何人有什么建议吗?

So anyone got any suggestions?

推荐答案

上的注意事项(2)。我曾经在的http://blog.kowalczyk.info/article/Forcing-basic-http-authentication-for-HttpWebReq.html注入Authorization头并且它.NET CF 2.0正常工作。

A note on (2). I used the method described at http://blog.kowalczyk.info/article/Forcing-basic-http-authentication-for-HttpWebReq.html to inject an authorization header and it works fine in .NET CF 2.0.

public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
    string authInfo = userName + ":" + userPassword;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
}

这篇关于.NET CF 2.0 HttpWebRequest的pre-认证和第一个请求发送凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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