Click here to Skip to main content
15,885,953 members
Home / Discussions / C#
   

C#

 
GeneralRe: problem registering in the access database Pin
ago248617-Mar-20 2:26
ago248617-Mar-20 2:26 
GeneralRe: problem registering in the access database Pin
Richard MacCutchan17-Mar-20 2:36
mveRichard MacCutchan17-Mar-20 2:36 
QuestionJava Api Callbacks crashes on dotnet framework version 4.5 and above Pin
isaketranjan16-Mar-20 0:06
isaketranjan16-Mar-20 0:06 
AnswerRe: Java Api Callbacks crashes on dotnet framework version 4.5 and above Pin
Gerry Schmitz16-Mar-20 5:05
mveGerry Schmitz16-Mar-20 5:05 
QuestionC# and ODATA CRUD communication with D365BC web service Pin
clemenslinders12-Mar-20 0:13
clemenslinders12-Mar-20 0:13 
SuggestionRe: C# and ODATA CRUD communication with D365BC web service Pin
Richard Deeming12-Mar-20 1:12
mveRichard Deeming12-Mar-20 1:12 
GeneralRe: C# and ODATA CRUD communication with D365BC web service Pin
clemenslinders12-Mar-20 3:48
clemenslinders12-Mar-20 3:48 
GeneralRe: C# and ODATA CRUD communication with D365BC web service Pin
Richard Deeming12-Mar-20 4:29
mveRichard Deeming12-Mar-20 4:29 
I was referring to the code from your original post, where you're constructing a JSON string manually:
Quote:
C#
string body = "{" + Environment.NewLine +
                    "\"No\":" + tbNo.Text + "," + Environment.NewLine +
                    "\"First_name\":\"" + tbFirstName.Text + "\"," + Environment.NewLine +
                    "\"Last_Name\":\"" + tbLastName.Text + "\"," + Environment.NewLine +
                    "\"FunctionName\":\"" + tbFunctionName.Text + "\"," + Environment.NewLine +
                  "}";

byte[] data = Encoding.ASCII.GetBytes(body);

try
{
    _request.ContentLength = data.Length;

    Stream requestStream = _request.GetRequestStream();
    requestStream.Write(data, 0, data.Length);
    requestStream.Close();

    HttpWebResponse _response = _request.GetResponse() as HttpWebResponse;//Here we get the exception errors 400 or 405
    Console.WriteLine(_response.StatusCode);
}
catch (Exception ex)
{

}
You don't need to worry about the JSON serialization with the HttpClient and the PostAsJsonAsync method - it handles the serialization for you.

You're getting a 401 error with your new code because you're passing the wrong values to the AuthenticationHeaderValue. The first parameter is the scheme, which should be "BASIC", and the second is the parameter, which should be the combined username and password.
AuthenticationHeaderValue Constructor (System.Net.Http.Headers) | Microsoft Docs[^]
C#
string _userName = "UserName";
string _wsKey = "Password";
byte[] authenticationParameter = Encoding.UTF8.GetBytes(_userName + ":" + _wsKey);
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationParameter));




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: C# and ODATA CRUD communication with D365BC web service Pin
clemenslinders12-Mar-20 4:54
clemenslinders12-Mar-20 4:54 
GeneralRe: C# and ODATA CRUD communication with D365BC web service Pin
Richard Deeming12-Mar-20 5:43
mveRichard Deeming12-Mar-20 5:43 
GeneralRe: C# and ODATA CRUD communication with D365BC web service Pin
clemenslinders12-Mar-20 21:25
clemenslinders12-Mar-20 21:25 
QuestionC# code -Compare 2 pdf and give output as 2 pdfs with color Pin
Member 1476942610-Mar-20 20:13
Member 1476942610-Mar-20 20:13 
AnswerRe: C# code -Compare 2 pdf and give output as 2 pdfs with color Pin
OriginalGriff10-Mar-20 20:49
mveOriginalGriff10-Mar-20 20:49 
AnswerRe: C# code -Compare 2 pdf and give output as 2 pdfs with color Pin
Maciej Los10-Mar-20 21:25
mveMaciej Los10-Mar-20 21:25 
JokeRe: C# code -Compare 2 pdf and give output as 2 pdfs with color Pin
Richard Deeming11-Mar-20 0:37
mveRichard Deeming11-Mar-20 0:37 
GeneralRe: C# code -Compare 2 pdf and give output as 2 pdfs with color Pin
kalberts12-Mar-20 3:01
kalberts12-Mar-20 3:01 
Questiontime requirements in real-times systems? Pin
auting8210-Mar-20 3:08
auting8210-Mar-20 3:08 
AnswerRe: time requirements in real-times systems? Pin
OriginalGriff10-Mar-20 3:49
mveOriginalGriff10-Mar-20 3:49 
GeneralRe: time requirements in real-times systems? Pin
auting8210-Mar-20 4:43
auting8210-Mar-20 4:43 
GeneralRe: time requirements in real-times systems? Pin
OriginalGriff10-Mar-20 4:51
mveOriginalGriff10-Mar-20 4:51 
AnswerRe: time requirements in real-times systems? Pin
Gerry Schmitz10-Mar-20 6:30
mveGerry Schmitz10-Mar-20 6:30 
AnswerRe: time requirements in real-times systems? Pin
Richard MacCutchan10-Mar-20 7:02
mveRichard MacCutchan10-Mar-20 7:02 
Questionimport multiple csv files into msql database - C# coding - reg Pin
Member 137889379-Mar-20 7:28
Member 137889379-Mar-20 7:28 
AnswerRe: import multiple csv files into msql database - C# coding - reg Pin
phil.o9-Mar-20 8:25
professionalphil.o9-Mar-20 8:25 
QuestionHow to invoke Web API in console or service application? Pin
meeram399-Mar-20 3:38
professionalmeeram399-Mar-20 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.