R
Roy Chastain
I have the following code.
Basically it attempts to connect without authentication and if that fails, it is to attempt supplied and/or default credentials.
It does not work. If I capture the traffic on the network, the code below only results in 1 GET Request being sent to the server
even though it does go through the loop and the call to wc_request.GetResponse() is done 3 times as expected.
See line with <------ If I remove the null and use CredentialCache.DefaultCredentials it works on the first pass through the
loop, but I don't really want to do that.
What are the correct steps to get the HttpWebRequest to actually send the 2nd and 3rd GET Request packets?
(PS. Are the error_response.Close() actually doing anything useful?)
private void TryConnect ()
{
HttpWebResponse error_response;
bool authorized = false;
while (! authorized)
{
try
{
if (Authorization == ConnectAuthorization_Type.CA_Provided)
if (Credentials != null)
wc_request.Credentials = Credentials;
else
{
Authorization = ConnectAuthorization_Type.CA_Default;
wc_request.Credentials = CredentialCache.DefaultCredentials;
}
else
if (Authorization == ConnectAuthorization_Type.CA_Default)
wc_request.Credentials = CredentialCache.DefaultCredentials;
else
wc_request.Credentials = null; //CredentialCache.DefaultCredentials; <---------
wc_response = (HttpWebResponse)(wc_request.GetResponse());
authorized = true;
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
error_response = (HttpWebResponse)ex.Response;
if (error_response.StatusCode == HttpStatusCode.Unauthorized)
{
if (Authorization == ConnectAuthorization_Type.CA_Default)
{
error_response.Close();
throw ex;
}
if (Authorization == ConnectAuthorization_Type.CA_None)
{
error_response.Close();
Authorization = ConnectAuthorization_Type.CA_Provided;
}
else
if (Authorization == ConnectAuthorization_Type.CA_Provided)
{
error_response.Close();
Authorization = ConnectAuthorization_Type.CA_Default;
}
continue;
}
}
throw ex;
}
}
} /* method WebConnect TryConnect */
Basically it attempts to connect without authentication and if that fails, it is to attempt supplied and/or default credentials.
It does not work. If I capture the traffic on the network, the code below only results in 1 GET Request being sent to the server
even though it does go through the loop and the call to wc_request.GetResponse() is done 3 times as expected.
See line with <------ If I remove the null and use CredentialCache.DefaultCredentials it works on the first pass through the
loop, but I don't really want to do that.
What are the correct steps to get the HttpWebRequest to actually send the 2nd and 3rd GET Request packets?
(PS. Are the error_response.Close() actually doing anything useful?)
private void TryConnect ()
{
HttpWebResponse error_response;
bool authorized = false;
while (! authorized)
{
try
{
if (Authorization == ConnectAuthorization_Type.CA_Provided)
if (Credentials != null)
wc_request.Credentials = Credentials;
else
{
Authorization = ConnectAuthorization_Type.CA_Default;
wc_request.Credentials = CredentialCache.DefaultCredentials;
}
else
if (Authorization == ConnectAuthorization_Type.CA_Default)
wc_request.Credentials = CredentialCache.DefaultCredentials;
else
wc_request.Credentials = null; //CredentialCache.DefaultCredentials; <---------
wc_response = (HttpWebResponse)(wc_request.GetResponse());
authorized = true;
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
error_response = (HttpWebResponse)ex.Response;
if (error_response.StatusCode == HttpStatusCode.Unauthorized)
{
if (Authorization == ConnectAuthorization_Type.CA_Default)
{
error_response.Close();
throw ex;
}
if (Authorization == ConnectAuthorization_Type.CA_None)
{
error_response.Close();
Authorization = ConnectAuthorization_Type.CA_Provided;
}
else
if (Authorization == ConnectAuthorization_Type.CA_Provided)
{
error_response.Close();
Authorization = ConnectAuthorization_Type.CA_Default;
}
continue;
}
}
throw ex;
}
}
} /* method WebConnect TryConnect */