Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a web application that contain page I should fill one dropdown list with data from JSON file through API link

I have the link and user name add password from the other application developer ( PHP )

Please help ...


What I have tried:

I found some solution but all of them don't have the option to pass user name and password
Posted
Updated 4-Nov-20 6:04am
Comments
Richard Deeming 4-Nov-20 5:31am    
We can't tell you, because we don't know how the API expects you to pass the username and password. You'll need to ask the PHP developer to provide more details.
maidrous 4-Nov-20 10:03am    
I need the way in asp.net to get this data from API as per the URL they gave me with Basic Authentication.
I don't think this related to the PHP developer because he gave the URL to get the JSON file.

Please update me
Richard Deeming 4-Nov-20 10:04am    
There's the missing information - you need to use Basic authentication[^].
maidrous 4-Nov-20 11:34am    
How to use Basic Authentication to get data from JSON file ??
and what is the missing information ?
Richard Deeming 4-Nov-20 11:36am    
It depends on the class you're using to load the file. A Google search for the class name + "basic authentication" should show you what you need to do.

1 solution

Quote:
I don't know what is the class I should use to get the data
Assuming you're on a recent version of .NET, try the HttpClient Class[^].
C#
string TargetUrl = "..."; // The URL of the page you want to load
string Username = "...";  // The username you were told to use
string Password = "...";  // The associated password

var client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(Username + ":" + Password);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

using (var response = await client.GetAsync(TargetUrl))
{
    response.EnsureSuccessStatusCode();
    
    string json = await response.Content.ReadAsStringAsync();
    ... DO SOMETHING WITH THE JSON HERE ...
}
If you're using .NET Framework 4.5 or 4.6, you'll need to add a reference to the NuGet package:
NuGet Gallery | System.Net.Http 4.3.4[^]
 
Share this answer
 
Comments
maidrous 5-Nov-20 1:53am    
Thanks Richard Deeming,

But can you convert the following to vb.net ? I thinks something missing in response declaration

using (var response = await client.GetAsync(TargetUrl))
{
response.EnsureSuccessStatusCode();

string json = await response.Content.ReadAsStringAsync();
... DO SOMETHING WITH THE JSON HERE ...
}
Richard Deeming 5-Nov-20 4:33am    
Using response As HttpResponseMessage = Await client.GetAsync(TargetUrl)
    response.EnsureSuccessStatusCode()
    Dim json As String = Await response.Content.ReadAsStringAsync()
    ... DO SOMETHING WITH THE JSON HERE ...
End Using
maidrous 5-Nov-20 7:15am    
OKHow can I store the data from JSON in data table to use it in dropdown list ?
Richard Deeming 5-Nov-20 7:22am    
Parse the JSON and use it as required. I can't tell you how to do that, because I have no idea what the JSON looks like or how you're trying to use it.

Either way, it sounds like a new question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900