cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Translate the entire conversation x

401 unauthorized

ST_7256598
2-Explorer

401 unauthorized

Hello,

 

I have a problem with an HTTP request. When the login has an “@”, I can't communicate via HTTP.

I've tried different types of encoding:
- ISO-8859-1
- Windows-1252
- UTF-8
- UTF-16
- UTF-32

with “Basic” authorization.

Here is my vb code:

 

 

Dim reader As IO.StreamReader
Try
Dim hwebrequest As System.Net.WebRequest = System.Net.WebRequest.Create(url)
hwebrequest.Method = "GET"
hwebrequest.Timeout = timeout

'Test encodage
Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(login + ":" + pass))
'Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("Windows-1252").GetBytes(System.Net.WebUtility.UrlEncode(login) + ":" + System.Net.WebUtility.UrlEncode(pass)))
'Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(login + ":" + pass))
'Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-16").GetBytes(System.Net.WebUtility.UrlEncode(login) + ":" + System.Net.WebUtility.UrlEncode(pass)))
'Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-32").GetBytes(System.Net.WebUtility.UrlEncode(login) + ":" + System.Net.WebUtility.UrlEncode(pass)))

hwebrequest.PreAuthenticate = True
hwebrequest.Headers.Add("Authorization", "Basic " + encoded)

 


Dim hwebresponse As System.Net.WebResponse = hwebrequest.GetResponse
reader = New IO.StreamReader(hwebresponse.GetResponseStream)
Dim res As String = ""
res = reader.ReadToEnd()
reader.Close()
hwebresponse.Close()
Return res
Catch ex As Exception
Return ex.ToString
End Try

 

 

This code works without problem when the login does not have an “@”. But I'd like to make it work with an “@” in it.

Thanks in advance

3 REPLIES 3

This is just AI answer:

When sending an "@" symbol in an HTTP login, it needs to be encoded as "%40" using standard URL encoding, which means that whenever you need to include an "@" in a username or URL, it should be replaced with "%40" to ensure proper interpretation by the server.

For example, if my pass word is Windchill@jj123, when I transform it to Windchill%40jj123, I again have the 401 unauthorized. May be I can use an pre authentification?


Hi @ST_7256598,

I wanted to see if you got the help you needed.

If so, please mark the appropriate reply as the Accepted Solution or please feel free to detail in a reply what has helped you and mark it as the Accepted Solution. It will help other members who may have the same question.

Of course, if you have more to share on your issue, please pursue the conversation.

Thanks,


Catalina
PTC Community Moderator
PTC
Announcements

Top Tags