0

I have one URL with some special characters like | and &. URL is returning JSON data.

When I am trying this URL in browser it will run and return json data but when I am trying using WebClient.DownloadString(), it will not work.

Example :

Using Browser :

http://websvr.test.com/abc.aspx?Action=B&PacketList=116307638|1355.00

Output : 
[{"Column1":106,"Column2":"Buying Successfully."}]

Using WebClient.DownloadString():

    using (WebClient wc = new WebClient())
    {
       var json = wc.DownloadString("http://websvr.test.com/abc.aspx?Action=B&PacketList=116307638|1355.00");

    }

Output : 
[{"Column1":-107,"Column2":"Invalid Parametrer Required-(RefNo|JBPrice)!"}]
4
  • 2
    show code please, else no one can help you Commented Aug 29, 2016 at 8:42
  • Welcome to StackOverflow, I think you should clarify your question a bit with examples of what you have tried so far, and what output you are expecting. See how to ask a question. Commented Aug 29, 2016 at 8:44
  • Could you please take a look at: stackoverflow.com/questions/5566942/… How to get a json string from url? Commented Aug 29, 2016 at 8:44
  • I think I've a similar problem, see stackoverflow.com/questions/72715042/…. Did you found a solution for it? Commented Jun 22, 2022 at 15:13

2 Answers 2

1

You should encode PacketList parameter in your URL, because it includes pipe character, which must be encoded to %7c. Browsers automatically encode necessary characters in URL, but you should encode it in code manually.

    var json = wc.DownloadString("http://websvr.test.com/abc.aspx?Action=B&PacketList=" + 
                  System.Web.HttpUtility.UrlEncode("116307638|1355.00");
Sign up to request clarification or add additional context in comments.

Comments

0

Try to set webclient's encoding before call DownloadString() and encode url using UrlEncode Method :

WebClient.Encoding = Encoding.UTF8;
var url = WebUtility.UrlEncode("http://websvr.test.com/abc.aspx?Action=B&PacketList=116307638|1355.00");
var json = wc.DownloadString(url);

3 Comments

I am also trying WebClient.Encoding = Encoding.UTF8; but not working.
@NikhilSangani Blanl value? But in the question you wrote that you got: [{"Column1":-107,"Column2":"Invalid Parametrer Required-(RefNo|JBPrice)!"}]. So, what did you get in response?
sorry MegaTron, I got [{"Column1":-107,"Column2":"Invalid Parametrer Required-(RefNo|JBPrice)!"}], but when i put url in browser it works fine. i think problem is " | " character.

Your Answer

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.