在内部网络上运行代码时出现不需要的代理身份验证问题。代码问题?
我编写了一个测试应用程序来根据内部 RESTful 服务验证一些用户详细信息。
如果我直接通过浏览器发出请求,我会收到来自服务的响应,但如果我通过代码发出请求,响应会指出我需要代理服务器身份验证。
虽然我可以提供用户可配置的设置,以便可以传递代理参数,但感觉不对,而且我不确定我的代码是否不正确。
下面是失败的代码片段,后面是包含代理详细信息的代码片段。
/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
bool valid = false;
try
{
// Create the XML to be passed as the request
XElement root = BuildRequestXML("LOGON");
// Add the action to the service address
Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");
// Make the RESTful request to the service using a POST
using (HttpResponseMessage response = new HttpClient().Post(serviceReq, HttpContentExtensions.CreateDataContract(itrent)))
{
// Retrieve the response for processing
response.Content.LoadIntoBuffer();
string returned = response.Content.ReadAsString();
// TODO: parse the response string for the required data
}
}
catch (Exception ex)
{
Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
valid = false;
}
return valid;
}
现在对于可以工作但有问题的块......
/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
bool valid = false;
try
{
// Create the XML to be passed as the request
XElement root = BuildRequestXML("LOGON");
// Add the action to the service address
Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");
// Create the client for the request
using (HttpClient client = new HttpClient())
{
// Create a proxy to get around the network issues and assign it to the http client
WebProxy px = new WebProxy( <Proxy server address>, <Proxy Server Port> );
px.Credentials = new NetworkCredential( <User Name>, <Password>, <Domain> );
client.TransportSettings.Proxy = px;
// Mare the RESTful request
HttpResponseMessage response = client.Post(serviceReq, HttpContentExtensions.CreateDataContract(root));
// Retrieve the response for processing
response.Content.LoadIntoBuffer();
string returned = response.Content.ReadAsString();
// TODO: parse the response string for the required data
}
}
catch (Exception ex)
{
Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
valid = false;
}
return valid;
}
非常感谢所有建议。 干杯。
I have written a test application to validate some user details against an internal RESTful service.
If I make the request directly through a browser I get a response from the service BUT if I make the request via my code, the response states that I need proxy server authentication.
Whilst I could provide user configurable settings so that the proxy parameters could be passed it just feels wrong and I'm not sure if my code is incorrect.
Below is the code snippet that fails, followed by the snippet with the proxy details.
/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
bool valid = false;
try
{
// Create the XML to be passed as the request
XElement root = BuildRequestXML("LOGON");
// Add the action to the service address
Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");
// Make the RESTful request to the service using a POST
using (HttpResponseMessage response = new HttpClient().Post(serviceReq, HttpContentExtensions.CreateDataContract(itrent)))
{
// Retrieve the response for processing
response.Content.LoadIntoBuffer();
string returned = response.Content.ReadAsString();
// TODO: parse the response string for the required data
}
}
catch (Exception ex)
{
Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
valid = false;
}
return valid;
}
Now for the chunk that works but has issues...
/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
bool valid = false;
try
{
// Create the XML to be passed as the request
XElement root = BuildRequestXML("LOGON");
// Add the action to the service address
Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");
// Create the client for the request
using (HttpClient client = new HttpClient())
{
// Create a proxy to get around the network issues and assign it to the http client
WebProxy px = new WebProxy( <Proxy server address>, <Proxy Server Port> );
px.Credentials = new NetworkCredential( <User Name>, <Password>, <Domain> );
client.TransportSettings.Proxy = px;
// Mare the RESTful request
HttpResponseMessage response = client.Post(serviceReq, HttpContentExtensions.CreateDataContract(root));
// Retrieve the response for processing
response.Content.LoadIntoBuffer();
string returned = response.Content.ReadAsString();
// TODO: parse the response string for the required data
}
}
catch (Exception ex)
{
Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
valid = false;
}
return valid;
}
All suggestions are gratefully received.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您浏览到该服务时,您正在使用登录用户的代理设置。当您尝试通过 Web 服务通过代理时,它可能以未配置代理设置的 ASP.NET 帐户运行。
您可以按照
此处对此进行了很好的讨论
When you browse to the service you are using the proxy settings for the signed on user. When you try to go through a proxy through the web service, it is probably running as the ASP.NET account which does not have proxy settings configured.
You can either
<system.net> <defaultProxy useDefaultCredentials="true"> <proxy usesystemdefault="true"/> </defaultProxy> </system.net>
Good discussion on it here
将此代码放入您的 app.config 中。它应该禁用代理
Put this code to your app.config. It should disable proxy