使用创建的 HttpWebResponse 接口 Returns 401 未授权错误
Using Created HttpWebResponse Interface Returns 401 Unauthorized Error
所以我最初有这个网络请求代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
为了添加单元测试,我将其更改为:
IHttpWebRequest request = IHttpWebRequestFactory.Create(uri);
request.UseDefaultCredentials = true;
IWebResponse response = request.GetResponse();
IHttpWebRequest:
public interface IHttpWebRequest
{
bool UseDefaultCredentials { get; set; }
IWebResponse GetResponse();
}
MyHttpWebRequest:
public class MyHttpWebRequest : IHttpWebRequest
{
private readonly HttpWebRequest httpWebRequest;
private bool useDefaultCredentials;
public MyHttpWebRequest(HttpWebRequest httpWebRequest)
{
this.httpWebRequest = httpWebRequest;
}
public bool UseDefaultCredentials
{
get { return useDefaultCredentials; }
set { useDefaultCredentials = value; }
}
public IWebResponse GetResponse()
{
return new MyWebResponse((HttpWebResponse)httpWebRequest.GetResponse());
}
}
IHttpWebRequestFactory:
public interface IHttpWebRequestFactory
{
IHttpWebRequest Create(string Uri);
}
HttpWebRequestFactory:
public class HttpWebRequestFactory : IHttpWebRequestFactory
{
public IHttpWebRequest Create(string Uri)
{
return new MyHttpWebRequest((HttpWebRequest)WebRequest.Create(Uri));
}
}
IWebResponse:
public interface IWebResponse
{
Stream GetResponseStream();
}
我的网络响应:
public class MyWebResponse : IWebResponse
{
private WebResponse webResponse;
public MyWebResponse(HttpWebResponse webResponse)
{
this.webResponse = webResponse;
}
public Stream GetResponseStream()
{
return webResponse.GetResponseStream();
}
}
但是网站现在 returns 出现 HTTP 错误 401 未授权。不过使用 Fiddler 是可行的。我也尝试设置request.Credentials = CredentialCache.DefaultNetworkCredentials
、request.PreAuthenticate = true
等,但没有用。 :(
我也尝试向 Credentials 提供我的用户名和密码,但未授权的错误仍然出现,这很奇怪,因为上面的原始代码有效。 :(
您没有正确使用 useDefaultCredentials
,试试这个:
MyHttpWebRequest:
public class MyHttpWebRequest : IHttpWebRequest
{
private readonly HttpWebRequest httpWebRequest;
public MyHttpWebRequest(HttpWebRequest httpWebRequest)
{
this.httpWebRequest = httpWebRequest;
}
public bool UseDefaultCredentials
{
get { return this.httpWebRequest.UseDefaultCredentials; }
set { this.httpWebRequest.UseDefaultCredentials = value; }
}
public IWebResponse GetResponse()
{
return new MyWebResponse((HttpWebResponse)httpWebRequest.GetResponse());
}
}
所以我最初有这个网络请求代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
为了添加单元测试,我将其更改为:
IHttpWebRequest request = IHttpWebRequestFactory.Create(uri);
request.UseDefaultCredentials = true;
IWebResponse response = request.GetResponse();
IHttpWebRequest:
public interface IHttpWebRequest
{
bool UseDefaultCredentials { get; set; }
IWebResponse GetResponse();
}
MyHttpWebRequest:
public class MyHttpWebRequest : IHttpWebRequest
{
private readonly HttpWebRequest httpWebRequest;
private bool useDefaultCredentials;
public MyHttpWebRequest(HttpWebRequest httpWebRequest)
{
this.httpWebRequest = httpWebRequest;
}
public bool UseDefaultCredentials
{
get { return useDefaultCredentials; }
set { useDefaultCredentials = value; }
}
public IWebResponse GetResponse()
{
return new MyWebResponse((HttpWebResponse)httpWebRequest.GetResponse());
}
}
IHttpWebRequestFactory:
public interface IHttpWebRequestFactory
{
IHttpWebRequest Create(string Uri);
}
HttpWebRequestFactory:
public class HttpWebRequestFactory : IHttpWebRequestFactory
{
public IHttpWebRequest Create(string Uri)
{
return new MyHttpWebRequest((HttpWebRequest)WebRequest.Create(Uri));
}
}
IWebResponse:
public interface IWebResponse
{
Stream GetResponseStream();
}
我的网络响应:
public class MyWebResponse : IWebResponse
{
private WebResponse webResponse;
public MyWebResponse(HttpWebResponse webResponse)
{
this.webResponse = webResponse;
}
public Stream GetResponseStream()
{
return webResponse.GetResponseStream();
}
}
但是网站现在 returns 出现 HTTP 错误 401 未授权。不过使用 Fiddler 是可行的。我也尝试设置request.Credentials = CredentialCache.DefaultNetworkCredentials
、request.PreAuthenticate = true
等,但没有用。 :(
我也尝试向 Credentials 提供我的用户名和密码,但未授权的错误仍然出现,这很奇怪,因为上面的原始代码有效。 :(
您没有正确使用 useDefaultCredentials
,试试这个:
MyHttpWebRequest:
public class MyHttpWebRequest : IHttpWebRequest
{
private readonly HttpWebRequest httpWebRequest;
public MyHttpWebRequest(HttpWebRequest httpWebRequest)
{
this.httpWebRequest = httpWebRequest;
}
public bool UseDefaultCredentials
{
get { return this.httpWebRequest.UseDefaultCredentials; }
set { this.httpWebRequest.UseDefaultCredentials = value; }
}
public IWebResponse GetResponse()
{
return new MyWebResponse((HttpWebResponse)httpWebRequest.GetResponse());
}
}