WebClient.DownloadString doesn't return value - c#

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)!"}]

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");

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);

Related

UTF-8 URL Encode

I am having issues in encoding my query params using HttpUtility.UrlEncode() it is not getting converted to UTF-8.
query["agent"] = HttpUtility.UrlEncode("{\"mbox\":\"mailto: UserName#company.com\"}");
I tried using the overload method and passed utf encoding but still it is not working.
expected result:
?agent=%7B%22mbox%22%3A%22mailto%3AUserName%40company.com%22%7D
Actual Result:
?agent=%257b%2522mbox%2522%253a%2522mailto%253aUserName%2540company.com%2522%257d
public StatementService(HttpClient client, IConfiguration conf)
{
configuration = conf;
var BaseAddress = "https://someurl.com/statements?";
client.BaseAddress = new Uri(BaseAddress);
client.DefaultRequestHeaders.Add("Custom-Header",
"customheadervalue");
Client = client;
}
public async Task<Object> GetStatements(){
var query = HttpUtility.ParseQueryString(Client.BaseAddress.Query);
query["agent"] = HttpUtility.UrlEncode( "{\"mbox\":\"mailto:UserName#company.com\"}");
var longuri = new Uri(Client.BaseAddress + query.ToString());
var response = await Client.GetAsync(longuri);
response.EnsureSuccessStatusCode();
using var responseStream = await response.Content.ReadAsStreamAsync();
dynamic statement = JsonSerializer.DeserializeAsync<object>(responseStream);
//Convert stream reader to string
StreamReader JsonStream = new StreamReader(statement);
string JsonString = JsonStream.ReadToEnd();
//convert Json String to Object.
JObject JsonLinq = JObject.Parse(JsonString);
// Linq to Json
dynamic res = JsonLinq["statements"][0].Select(res => res).FirstOrDefault();
return await res;
}
The method HttpUtility.ParseQueryString internally returns a HttpValueCollection. HttpValueCollection.ToString() already performs url encoding, so you don't need to do that yourself. If you do it yourself, it is performed twice and you get the wrong result that you see.
I don't see the relation to UTF-8. The value you use ({"mbox":"mailto: UserName#company.com"}) doesn't contain any characters that would look different in UTF-8.
References:
HttpValueCollection and NameValueCollection
ParseQueryString source
HttpValueCollection source
I strongly suggest you this other approach, using Uri.EscapeDataString method. This method is inside System.Net instead of System.Web that is a heavy dll. In addition HttpUtility.UrlEncode encode characters are in uppercase this would be an issue in certain cases while implementing HTTP protocols.
Uri.EscapeDataString("{\"mbox\":\"mailto: UserName#company.com\"}")
"%7B%22mbox%22%3A%22mailto%3A%20UserName%40company.com%22%7D"

Converting the Special characters in HttpContext to string

I have the below code in a webservice to read the post data. The issue here is whenever the request contains special character, lets say "amé", the character is replaced as am� while converted to string.
byte[] postData= HttpContext.Request.BinaryRead(HttpContext.Request.ContentLength);
string strReq = Encoding.UTF8.GetString(postData);
And I call the WebService with the below code:
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "text/xml; charset=utf-8";
webClient.Headers[HttpRequestHeader.Authorization] = credentials;
string output = webClient.UploadString(url, "POST", input);
You'll need to specify in your web service that the post data is UTF-8 encoded, otherwise you won't be able to decode it as UTF-8. Adding charset=utf-8 to the end of your content-type header should do the trick.
e.g.
'content-type': 'text/xml; charset=utf-8'
Note that specifications for JSON and form encoded POST data require UTF-8.

ISO-8859 encode post request content C#

I am trying to send a POST request in C# with a parameter encoded to ISO-8859. I am using this code:
using (var wb = new WebClient())
{
var encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
var encodedText = System.Web.HttpUtility.UrlEncode("åæ ÆÆ øØ ø", encoding);
wb.Encoding = encoding;
wb.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var data = new NameValueCollection();
data["TXT"] = encodedText;
var response = wb.UploadValues(_url, "POST", data);
}
I have figured out that the correctly encoded string for "åæ ÆÆ øØ ø" is %E5%E6+%C6%C6++%F8%D8+%F8, and I can see when debugging that encodedText actually is this string. However when inspecting the raw request in fiddler, I can see that the string looks like this: TXT=%25e5%25e6%2B%25c6%25c6%2B%25f8%25d8%2B%25f8. I am guessing some kind of extra encoding is being done to the string after or during the call to UploadValues().
Thank you so much in advance.
I checked Google for this. According to another question here on SO at UTF32 for WebClient.UploadValues? (second answer), Webclient.UploadValues() indeed does encoding itself. However, it does ASCII encoding. Youll have to use another method to upload this, like HttpWebRequest.

Prevent Encoding when converting to byte Array

I have a controller that is building using a StringBuilder called param to create a string to post to a REST service. The string that is passed is a query string that will get parsed. I'm encoding the values for each pair so that ampersands, doesn't inadvertantly create a new key.
An encoded param string might look like
clientType=MyClient&form_id=webform_client_form_38&referrer=http://mywebsite&company=My+%26+Company
Here is the code I'm using to send to the REST service
byte[] formData = UTF8Encoding.Default.GetBytes(param.ToString());
req.ContentLength = formData.Length;
//Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
When I debug the REST service the string is always encoded back, adding the ampersand back between My and Company.
Is there another built in method that will convert the string to a byte without encoding the text?
You can't use a HTML Decode function? that will transform back the string to the correct string that you sent.
HttpUtility.HtmlDecode

How to Remove Back Slashes from JSON Response in C#?

I have written a webservice which converts the XML Response of a TP-Open Service into JSON format. I am new to WCF and writing Webservice.
But the converted JSON format displays as follow.
"{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"25\", \"humidity\": \"48\", \"observation_time..
How to Remove these back slashes \ and my code so far.
public string checkweather(string q, string num_of_day)
{
HttpWebRequest request=..;
...
string Url = string.Format("http://free.worldweatheronline.com/feed/weather.ashx?q={0}&format={1}&num_of_days={2}&key={3}", q, format, num_of_days, key);
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Url);
Request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
var result2=reader.ReadToEnd();
}}}
return result2;
}
Please inform me if you need more information.
I think that your JSON is fine, the backslashes are escaping the quotations, which people have said. The following code shows some valid XML -> Json converting. (Using Json.NET)
const string xml = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
XmlDocument node = new XmlDocument();
node.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(node);
If you view in debug mode, you will see the backslashes, but the output is valid Json.
Output
{"note":{"to":"Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}}
Are you sure there are backslashes in your string? It looks like me they are the escape characters because there are " characters in your string.
str = JToken.Parse(your string).ToString();
rest service time: System.Net.WebUtility.HtmlEncode(JsonString);
response time: System.Net.WebUtility.HtmlDecode(JsonString);
if your decoded code contains this string \\" then please replace \\ to \

Categories