Description
Sets the authentication credentials, assuming that a supported authentication scheme and user credentials are already known or obtained (for example, via GetSupportScheme).
This function only sets the authentication credentials. The credentials will NOT be sent to the server or proxy for validation until when SendRequest is executed.
Applies to
HTTPClient object, RestClient object
Syntax
objectname.SetCredentials(Boolean isProxyAuth, int scheme, string user, string password)
Argument |
Description |
---|---|
objectname |
The name of the object from which you want to set the authentication credentials. |
isProxyAuth |
Specifies whether the proxy or server requires authentication. Values are: True -- The proxy requires authentication. The HTTP status code is 407. False -- The server requires authentication. The HTTP status code is 401. |
scheme |
The authentication scheme that is currently used. Values are: 0 -- Automatic authentication (Automatically use one of the following five authentication schemes) 1 -- Negotiate authentication 2 -- NTLM authentication 3 -- Passport authentication (Unvalidated, because Microsoft has terminated this service.) 4 -- Digest authentication 5 -- Basic authentication * Integrated Windows authentication is not supported. |
user |
The user name for authentication. |
password |
The password for authentication. |
Return value
Integer.
Returns 1 if it succeeds and -1 if an error occurs (such as when argument is null, user name is of length 0, scheme is invalid etc.)
Examples
The following example specifies that the server requires authentication, uses the Negotiate authentication scheme and sets the user name and password.
string ls_User,ls_PassWord, ls_Url HTTPClient lhc_Client lhc_Client = Create HTTPClient ls_User = "guest" ls_PassWord = "guest" ls_Url = "https://testserver.com:8001/" lhc_Client.SetCredentials(False, 1, ls_user, ls_PassWord)
The following example creates an HTTPClient object, sets the credentials, and then sends the request to the server. The credentials will be sent to the server when SendRequest is executed. When credentials are successfully validated, GetResponseStatusCode will return 200.
string ls_User,ls_PassWord,ls_Url long ll_rtn,ll_Code HTTPClient lhc_Client lhc_Client = Create HTTPClient ls_User = "guest" ls_PassWord = "guest" ls_Url = "https://csharpserver.appeon.com:8001/" //sets credentials ll_rtn = lhc_Client.setcredentials( false,5, ls_User,ls_PassWord) messagebox("Basic SetCredentials( false, 0, user, pass )=",ll_rtn) //sends requests (including sending the credentials) ll_rtn = lhc_Client.SendRequest("GET", ls_Url ) ll_Code = lhc_Client.GetResponseStatusCode()
The following example gets the authorization scheme and then sets credentials accordingly.
HTTPClient lhc_Client lhc_Client = Create HTTPClient ls_User = "guest" ls_PassWord = "guest" ls_Url = "https://csharpserver.appeon.com:8001/" ll_rtn = lhc_Client.SendRequest( "Get",ls_Url) //get the supported scheme ll_rtn = lhc_Client.getsupportscheme(ref li_Scheme) if ll_rtn = 1 and UpperBound(li_Scheme) >= 1 Then ll_rtn = lhc_Client.SetCredentials(false, li_Scheme[1], ls_User, ls_PassWord) ll_rtn = lhc_Client.SendRequest("GET", ls_Url ) ll_Code = lhc_Client.GetResponseStatusCode() messagebox("SendRequest","code:"+string(ll_Code)) else messagebox("GetSupportScheme( Scheme )",ll_rtn) end if
See also