1

I want to add my string username and password into the string jsonData however there seems to be an error.

public async Task<string> authLogin(string username, string password)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://172.20.129.193/");

        string jsonData = @"{""AdminNo"" : """+username+""", """+password+""" : ""password""}";

        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync("NYPStudentLifeService/api/student/login",content);

        string result = await response.Content.ReadAsStringAsync();
        return result;
    }

2 Answers 2

2

The way you are using is wrong try out this method. Also the password order was wrong as mentioned above. ^

Instead of:

string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : """+password+"""}";

You can try out this:

string jsonData = @"{'AdminNo':'"+username+"','Password':'"+password+"'}";
Sign up to request clarification or add additional context in comments.

1 Comment

you can do this way too. string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : ""pass""}"; jsonData = jsonData.Replace("pass", Passwrd);
0

Use RestSharp for REST API calls and NewtoSoft to serialize your objects to JSONs. Your code is obsolete.

http://restsharp.org/

http://www.newtonsoft.com/json

By the way... your error is the order of "password"

You have

string jsonData = @"{""AdminNo"" : """+username+""", """+password+""" : ""password""}";

Should be

string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : """+password+"""}";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.