0

I am trying to read data from this API API Wykazu podatników VAT

Thru HttpWebRequest it's working:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://wl-test.mf.gov.pl:9091/wykaz-podatnikow/api/search/nips/3245174504?date=2019年09月27日");
 try
 {
 WebResponse response = request.GetResponse();
 using (Stream responseStream = response.GetResponseStream())
 {
 StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
 Console.WriteLine(reader.ReadToEnd());
 }
 }
 catch (WebException ex)
 {
 WebResponse errorResponse = ex.Response;
 using (Stream responseStream = errorResponse.GetResponseStream())
 {
 StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
 String errorText = reader.ReadToEnd();
 // log errorText
 }
 throw;
 }

But when I try it thru WebClient:

 var client = new WebClient();
 client.Headers.Add("nips", "3245174504");
 client.Headers.Add("date", "2019-09-27");
 var response = client.DownloadString("https://wl-test.mf.gov.pl:9091/wykaz-podatnikow/api/search/nips/");
 Console.WriteLine(response.ToString());

I got this error message:

Unhandled Exception: System.Net.WebException: An exception occurred during a WebClient request. ---> System.ArgumentException: The 'date' header must be modified using the appropriate property or method. Parameter name: name at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName) at System.Net.WebHeaderCollection.Add(String name, String value) at System.Net.HttpWebRequest.set_Headers(WebHeaderCollection value) at System.Net.WebClient.CopyHeadersTo(WebRequest request) at System.Net.WebClient.GetWebRequest(Uri address) at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) --- End of inner exception stack trace --- at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address) at System.Net.WebClient.DownloadString(String address)

How should I format the second Date parameter?

Prashant Pimpale
10.7k10 gold badges51 silver badges88 bronze badges
asked Sep 27, 2019 at 14:11
2
  • Are you sure you have to add these the parameters in the header? Once it is a "get" request it is supposed to be on the url like in the first example. Commented Sep 27, 2019 at 14:17
  • If you want to change the date parameter check this link: link Commented Sep 27, 2019 at 14:19

2 Answers 2

1

I would recommend using the newer HTTPClient as it is specifically created for API calls -

 using (var x = new HttpClient())
 {
 var y = x.GetAsync("https://wl-test.mf.gov.pl:9091/wykaz-podatnikow/api/search/nips/3245174504?date=2019年09月27日").Result;
 var json = y.Content.ReadAsStringAsync().Result;
 }

Just to add, please don't use .Result, instead use the async/await operators.

answered Sep 27, 2019 at 14:23

3 Comments

yes sorry edited that just did that for sample purpose only
Hi, what is that "date=2019年09月27日" is it a registered date of that number ?
0

Are you mistaking request headers with Query Parameters? if so you might want the following instead:

https://wl-test.mf.gov.pl:9091/wykaz-podatnikow/api/search?nips=3245174504&date=2019年09月27日

answered Sep 27, 2019 at 14:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.