更新时间:2022-10-14 22:39:19
到目前为止你有什么?您需要(我猜)基本的Web凭据,例如
using(WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(" username"," password");
// ..
}
Hello Everybody,
For the last few hours I've been trying to receive a simple JSON object from a rest https api that requires username and password.
The company provided a working example in Javasccript how to get the info. I don't know javascript, only c#, but I can't seem to make it work. Probably because I'm not familiar with wen http requests etc.
This is the working JS code:
function signin(){ username = $('#inputUsername').val(); pass = $('#inputPassword').val(); if(pass.length > 0 && username.length > 0){ //Build post request $.post( source+"/login", {username: username, password:pass}, function( data, textStatus, jqxhr ) { $("header div h4#header_username").html(username); token = data.auth; $('#main-content').show() $('#form-signin').hide() populateVehicles(); }); } }
Could anyone help me convert it to c#? I've been trying to use HttpClient or HttpRequest and each time I receive a 401 Unauthorized Error or Bad Request Error.
Thanks in advance.
Found the solution. Sending the credentials worked only like this. Hope it helps.
var data = new NameValueCollection(); data["username"] = "USERNAME"; data["password"] = "PASSWORD"; var response = wb.UploadValues("URL", "POST", data);
What have you so far? You need (I guess) basic web credentials, something like
using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential("username", "password"); //.. }