Im calling an API that returns part of a URL that I have to concatenate on code after.
The value im getting is:
"\"SYSTEM\/PRODUCTION\/TEMP\/preauthletter_7470D75C-594D-4132-BAEE-F7339B034DD8_110220201138.pdf\""
When I try to concatenate to my base URL the result is an invalid URL because of the previews extras slash and quotes.
var result = _reportEngine.GetPreAuthorizationLetter().Result;
// result is the URL part having the extra slashes
result = string.Format(#"http://cloud.demo.com/{0}", result);
Is there any .NET method that cleans this so I can get a valid URL.
The result expected should be:
http://cloud.demo.com/SYSTEM/PRODUCTION/TEMP/preauthletter_7470D75C-594D-4132-BAEE-F7339B034DD8_110220201138.pdf
UPDATE
The problem I guess is that API is returning as json instead of just pure string.
Found how to solve it, I just had to deserialize it into a string:
result = JsonConvert.DeserializeObject<string>(result);
And voila!!
Related
I am trying to read the response from WEB API, that returns string.
I use below statement:
response.Conent.ReadAsStringAsync().Result.ToString();
I was expecting the result to be "TEST" but I am getting ""TEST"".
How do I remove the extra characters from the output.
I ran the API alone in Postman, and getting expected result "TEST". But when I try to read the response via code, I am getting extra characters.
Not sure, where I am missing. Any help on this really appreciated.
Edit: Adding screenshot of the response
The correct way would be to use await to read the string result:
var result = await response.Conent.ReadAsStringAsync();
And you would not need to use .ToString() because the result is already a string.
To manually remove the double quotes from the output you can just trim it:
result = result.Trim('"');
I'm trying to do something like this so I can grab the information out of the URL from a c# webbrowser, I later break up the decoded url using & as an endpoint for the sections:
string theURL = System.Uri.UnescapeDataString(webBrowser1.Url.ToString().Replace("%26","and")).Replace('+',' ');
My problem is that it seems when I call .ToString() on the webBrowser1.Url it automatically converts %26 to & effectively ruining my URL. I need to preserve the %26 until I can handle it myself and replace it with something else.
--
I tried an alternative method like this as well but param1 always came back blank for some reason, I could never get anything out of it:
string queryString = webBrowser1.Url.Query;
var q = System.Web.HttpUtility.ParseQueryString(queryString);
var param1 = q["paramname"];
This also gave me blanks:
foreach (var element in q)
{
MessageBox.Show(element.ToString());
}
Try webBrowser1.Url.OriginalString. That should preserve the original URL.
The ToString method of System.Uri is overridden and returns the canonical representation. Check the example which just shows this in the documentation.
I'm trying to UrlEncode a web address using Uri.EscapeDataString, but the result isn't correct. Here's an example:
string url = "https://mega.co.nz/#!GVZFwAbB!NzdN2jp7A_WmQBLC4RJrCX8SzixFIEo7oZZARaMAmXQ";
string encodedUrl = Uri.EscapeDataString(url);
Expected result would be:
https%3a%2f%2fmega.co.nz%2f%23!GVZFwAbB!NzdN2jp7A_WmQBLC4RJrCX8SzixFIEo7oZZARaMAmXQ
But the actual one is:
https%253a%252f%252fmega.co.nz%252f%2523%21GVZFwAbB%21NzdN2jp7A_WmQBLC4RJrCX8SzixFIEo7oZZARaMAmXQ
As you can see, there's a bunch of extra %25s that don't belong there. Isn't %25 the encode for "%"? There are no %s in my original string... what's going on?
EDIT: I can't use the System.Web assembly for this project, so unfortunately I can't use the HttpUtility.UrlEncode() method for this.
Well, after searching around a bit more, it seems that this does the job, without relying on system web:
System.Net.WebUtility.UrlEncode(url);
The encoding is the correct one, without %25s.
Uri.EscapeDataString doesn't encode URL. Use HttpUtility.UrlEncode instead.
string url = "https://mega.co.nz/#!GVZFwAbB!NzdN2jp7A_WmQBLC4RJrCX8SzixFIEo7oZZARaMAmXQ";
string encodedUrl = HttpUtility.UrlEncode(url);
Result is:
https%3a%2f%2fmega.co.nz%2f%23!GVZFwAbB!NzdN2jp7A_WmQBLC4RJrCX8SzixFIEo7oZZARaMAmXQ
i have an ashx file that requires some query-string values to deliver appropriate images.
The problem is some search engines urlencode then htmlencode those urls in their cache or when they index those.
So for example instead of getting
/preview.ashx?file=abcd.jpg&root=small
i get
/preview.ashx?file=abcd.jpg&root=small
this basically throws off the context.Request.QueryString["root"] so it thinks that there's no variable root
i guess the second key in the querystring is amp;root i.e the part after the & sign.
What i'm wondering is if there's a way to automatically html and urldecode the querystring on serverside so that the program doesn't get confused.
There is no harm in calling HttpUtility.HtmlDecode or HttpUtility.UrlDecode more than once.
string queryString = "/preview.ashx?file=abcd.jpg&root=small";
var parsedString = HttpUtility.HtmlDecode(queryString);
var root = HttpUtility.ParseQueryString(parsedString)["root"];
To URL encode and decode you can use the following methods:
string encoded = System.Web.HttpUtility.UrlEncode(url);
string decoded = System.Web.HttpUtility.UrlDecode(url);
I've seen instances in the past where the Query String has been doubly encoded. In which case you'll need to doubly decode — this may be your issue...
I am using the Twitter API, and the response I get is in JSON format. When I parse JSON using C# so the value of profile_image_url does not contain the proper URL. This URL is absolutly fine in the response, but after parsing the response I get the following URL. How do I remove backslashes?
http://a1.twimg.com/profile_images/700049686/14_normal.jpg
You can remove backslashes using the Replace function:
url = url.Replace("\\", "")
But perhaps you ought to spend some time working out how those backslashes got there in the first place. It sounds like you aren't parsing the JSON correctly. What parser are you using?