Authentication Processing with HttpWebRequest (.NET)

I am accessing the REST web service from a VB application. I create an HttpWebResponse object and call GetRequestStream on it just fine, but when I call GetResponse, I get exception 401.

Here is my code (modified for publication):

Dim webRequest = DirectCast(WebRequest.Create(endpoint), HttpWebRequest)
webRequest.Method = "POST"
webRequest.ContentLength = contentLength
webRequest.ContentType = "application/x-www-form-urlencoded"

request.Credentials = New NetworkCredential(Username, Password)

' Works fine
Using stream As Stream = webRequest.GetRequestStream()
    stream.Write(data, 0, data.Length)
End Using

' Barfs
Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)

I get exception 401 as soon as I call "webRequest.GetResponse ()". The response has a “WWW-Authenticate” header with a corresponding scope. I tried setting "webRequest.PreAuthenticate = True" and also manually setting the header using "webRequest.Headers.Add (HttpRequestHeader.Authorization, _authheader)"

I can’t track these requests using Fiddler / Wireshark, etc., because it is SSL traffic, I’m sure there is a way to control it, I have not found it yet.

,

- Connor

+3
4

! , ! , , , , URL-, .

+1
+3

AUTHORIATION, - ...

string autorization = userName + ":" + password;
byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(autorization);
autorization = Convert.ToBase64String(binaryAuthorization);
autorization = "Basic " + autorization;
webRequest.Add("AUTHORIZATION", autorization);

, Credentials .

+3

, , , ,

request.ProtocolVersion = HttpVersion.Version10
0

Source: https://habr.com/ru/post/1711758/


All Articles