Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 3, 2016 6:56 PM
Hi All,
I am new to C# coding. I have a url something similar to https://data.group.com/dme/api/v1. when i click on it, it ask for username and password. we download json file through this url. how can i download json file through this url using C# code.
Thanks
Syed
All replies (6)
Thursday, November 3, 2016 7:30 PM | 1 vote
Try adjusting the code below as needed.
And here is HttpClient: https://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.118%29.aspx
public void CallAPI()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://data.group.com/dme/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//if credentials are needed
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Format("{0}:{1}", "username", "password")));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
var yourPostedObject = new object();
//pick one of these to use
var resultString = client.PostAsJsonAsync("v1", yourPostedObject).Result.Content.ReadAsStringAsync().Result;
var resultObject = client.PostAsJsonAsync("v1", yourPostedObject).Result.Content.ReadAsAsync<T>().Result;
}
}
Monday, November 7, 2016 1:44 AM
Hi SyedF,
Thank you for posting here.
For your question, please try the following code. For the https://data.group.com/dme/api/v1 you provided, I could not open it. If the following code could not solve your problem, please provide more details about your question and the url.
using (WebClient w = new WebClient())
{
var json = w.DownloadString("https://qiita.com/api/v1/tags.json?page=1&per_page=100");
}
I hope this would be helpful to you.
If you have something else, please feel free to contact us.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Monday, November 7, 2016 4:12 PM
I need to pass username and password also. How can i pass username and password to authenticate it.
Monday, November 7, 2016 6:58 PM
Depends on the authentication used however in general if you are using WebClient it would be something like this.
WebClient client = new WebClient();
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("username", "password");
Depending on how robust your RESTful Consumption will be you may want to look at using some solutions already available via NUGET like Newtonsoft.JSON for JSON serialization/deserialization and RestSharp which is a Simple Rest and HTTP API Client.
Wednesday, November 9, 2016 9:04 AM | 1 vote
Hi SyedF,
Thank you for feedback.
**>>I need to pass username and password also. How can i pass username and password to authenticate it.
Are you the developer of url which you want to download json file? If you do not have the username and password, you could use Fiddler to catch the package and analyze the results to obtain more detailed debug information.
Through the information, you could simulate the request with the username and password to get the response and download the json file.
I hope this would be helpful to you.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Tuesday, December 4, 2018 5:04 PM
how to add cookie to webclient?