System.Net.WebRequest.Create and URL encoding - c#

I have to make a call to an API and the URL hasd this format: http://apiurl.com/method/parameter1/parameter2
Now, parameter2 may contain characters that could mess with the URL such as forward slashes, so I URL encode parameter2:
string parameter2 = HttpUtility.UrlEncode("string with a / in it");
System.Net.WebRequest request = System.Net.WebRequest.Create(String.Format("http://apiurl.com/method/{0}/{1}", parameter1, parameter2));
The problem is that if I inspect the URL of the request, parameter2 is NOT URL encoded and the call fails (unsurprisingly). However, when I inspect the value of parameter2 it is correctly URL encoded with the forward slash replaced with %2f
I am using .NET 3.5 so many of the usual workarounds don't apply (genericUriParserOptions for example is .NET 4 and above only)
Thanks

Related

Uri %20 converter problem when trying to build a HttpRequestMessage - C#

I'm trying to build a http request using HttpRequestMessage from .net. The problem is that my URL contains %20 in parameters, and I need to send specifically %20 not space caracter. But to use HttpRequestMessage I have to transform my string URL in URI, and then it converts the %20 in space.
I tried a few changes, using encoders before sending to httpRequestMessage. The strange part is that if I encode a "%" for example, it maintain the %25 value, but for spaces does't not.
Example of me testing Uri.ScapeDataString(), look how it keeps the correctly encoding from '['character, but it looses 'space' caracter:
Example with ScapeDataString
For me this is clearly a problem on Uri conversion.

How to pass url with query string as an api parameter?

I am curently integrating the api to my application. One of the parameter is content_url which is a valid HTML URL for content. How to I pass whole url with query string?
API Endpoint parameter is located below.
var contentUrl
= "https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml/OSMANLIKP/FXBULTEN/2020-01-20/null";
var url = "https://campaigns.zoho.com/api/v1.1/createCampaign?" +
"resfmt=json&" +
"campaignname=Campaign12&" +
"from_email=mert.metin#finnet.gen.tr&" +
"subject=Test Create Campaign 11&" +
"list_details=%7Bd6ef220cc42b50d7ddf3236cbb9b8847894375f7a7d2d66f%3A%5B%5D%2Cd6ef220cc42b50d71438a8b019c635e5894375f7a7d2d66f%3A%5B%5D%7D&" +
"content_url=" + contentUrl;
Content url is https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP&Kod=FXBULTEN&Tarih=2020-01-20
But I post url value, API gets the url until first & ampersand character. So remain parameter was missing. I need to pass whole url as a parameter.
Expected Result=https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP&Kod=FXBULTEN&Tarih=2020-01-20
Actual Result=https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP
My application is based on MVC and C# language. I also enclosed the url with double quotes but it is not work because of Api Response which is INVALID IMPORT URL.
You must encode the URL before passing as a parameter as
myURL="myURL1?redir=" + Server.URLEncode("myURL2?id=4&user=test");
It will create a correct URL without '?' and "&" characters as
myURL= myURL1?redir=myURL2%3id%34%26user%3dtest
To extract your redirect url from this encoded url you must use
HttpServerUtility.UrlDecode to turn it into a correct url again.

server.htmlencode of asp.net equivalent in asp.net mvc

I have a ASP.NET MVC controller which is making call to another service using HttpClient class.
var url = "some url";
var client = new HttpClient();
var result= client.GetAsync(url);
The URL I am sending contains some special characters. How can encode special characters in ASP.NET MVC controller?
Thanks!!1
Try this:
url = HttpUtility.UrlEncode(url);
As you are considering a URL that will be used as such for a request (with HttpClient.getAsync) -- not as an argument within another URL -- you should use Uri.EscapeUriString.
Here is a comparison of three methods for the following URL:
var url = "http://some url?data=x y+z&user=1#ok";
HttpUtility.UrlEncode
Console.WriteLine(HttpUtility.UrlEncode(url));
http%3a%2f%2fsome+url%3fdata%3dx+y%2bz%26user%3d1%23ok
Obviously, this is not desired: the URL got damaged with / escaped, a + entered in the path, ...etc. The method seems useful for the query part of the URL, but not for the whole lot.
HttpUtility.UrlPathEncode
Console.WriteLine(HttpUtility.UrlPathEncode(url));
http://some%20url?data=x y+z&user=1#ok
This looks useful, although the space is a bit of a problem in the query part (notice the broken hyperlinking here, although browser can deal with it). But more importantly, the method is being deprecated:
Do not use; intended only for browser compatibility. Use UrlEncode.
Uri.EscapeUriString
Console.WriteLine(Uri.EscapeUriString(url));
http://some%20url?data=x%20y+z&user=1#ok
This seems to do the job well: %20 is an escape sequence that all modern browsers should support, also when occurring in the query part of the URL.
There is no need it encoding in Razor View Engine starting from 3rd version, and it's very convenient. Instead if you want to use tags you should use:
#Html.Raw(myString)
So basically just using Razor comes with encoding by default.
You should use HttpUtility.UrlPathEncode
When you use url = HttpUtility.UrlEncode(url) it doesn't work fine with spaces.

Webmaster Tools API C# - Url Encoded SiteID causing 400 response

The Webmaster Tools API requires a SiteID for most operations.
This SiteID is a Url Encoded version of the site's url, as appears in the Google Webmaster Tools dashboard.
So why does the next URL doesn't work (the dreaded "Bad Request", or "Site Not Found")?
var site = "http://example.com/";
var urlEncoded = HttpUtility.UrlEncode(site);
var url = "https://www.google.com/webmasters/tools/feeds/" + urlEncoded + "/crawlissues/";
Google expects upper case letters for the encoded characters, while HttpUtility.UrlEncode produces lower case characters.
See this answer for a "selective ToUpper" method implementation.
(Another thing, the last slash might make a difference! http://x.com/ is not http://x.com)

C#: Sending HTTP GET request without UTF-8 encoding

I need to send HTTP GET request from C# to CLASSIC ASP service.
The service is built in a way that it decodes the data from the QueryString using Windows-1255 encoding, rather than the standard UTF-8.
It seems that HttpWebRequest class always encodes GET data with UTF-8 and it doesn't work for me. Is there any way to send HTTP GET request from C#, while GET data is encoded with different than UTF-8 encoding?
Thanks.
You need to set a header on your get request:
Content-Type:text/xml; Charset=windows-1255
HttpRequest r = new HttpRequest(.....);
r.Headers.Add("Content-Type", "text/xml; Charset=windows-1255");
Maybe this post will be of some use too:
Read non-english characters from http get request
Ok, I finally got the answer.
First of all, specifying ContentType in the header doesn't work.
If destination URL is containing none-English letters, the HttpWebRequest will always use UTF-8 + URLEncode to build the final URI the request is sent to.
To use encoding different from UTF-8 I needed to encode URL values by myself (instead of providing necessary encoding to HttpWebRequest as I expected).
Following function that builds HTTP GET URL, while values are encoded with any requested encoding (and not with the default UTF-8):
string BuildData(NameValueCollection getData, Encoding enc)
{
StringBuilder urldata = new StringBuilder();
for (int i = 0; i < getData.Count; i++)
{
if (i > 0) urldata.Append("&");
urldata.Append(getData.Keys[i] + "=" + HttpUtility.UrlEncode(enc.GetBytes(getData[i])));
}
return urldata.ToString();
}
The HttpWebRequest can be used with something like
"http://get-destination.com/submit?" + BuildData(keysAndValues, Encoding.GetEncoding(1255));
In this case HttpWebRequest gets already encoded URL which doesn't contain none-English letters and it keeps it as is.

Categories

Resources