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.