Reputation: 21
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)!"}]
Upvotes: 0
Views: 1495
Reputation: 24609
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);
Upvotes: 0
Reputation: 446
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");
Upvotes: 1