c# is WebRequest.Create gives me a URL encoded?

2019-07-25 20:57发布

问题:

I have been giving an API to send SMS, the API states that I should use this url

http://<server>:<port>/bulksms/bulksms?
username=XXXX&password=YYYYY&type=Y&dlr=Z&destination=QQQQQQQQQ&sour
ce=RRRR&message=SSSSSSSS<&url=KKKK>

Where message and url must be URL-UTF-8 encoded.

what I did is creating that url like this:

string urlSMS = string.Format("http://{0}:{1}/bulksms/bulksms?username={2}&password={3}&type={4}&dlr={5}&destination={6}&source={7}&message={8}&url=0",
server, port, username, password, type, dlr, destination, source, message);

then I called that URL like this:

WebRequest wr = WebRequest.Create(urlSMS);
                wr.Timeout = 3500;
                try
                {
                    HttpWebResponse response = (HttpWebResponse)wr.GetResponse();

however, I kept having timeout error message, I checked everything that is why i am asking. i need to know if

WebRequest.Create(urlSMS)

gives me a URL-UTF-8 encoded or not.

Thanks

回答1:

Yes. It looks like WebRequest.Create(url) will UTF-8 encode the url.

When I try this:

WebRequest r = WebRequest.Create("http://www.google.com/search?q=♥");

I can see that the internal representation if the URI is encoded: r.RequestUri.AbsoluteUri returns http://www.google.com/search?q=%E2%99%A5

Whereas r.RequestUri.ToString() returns http://www.google.com/search?q=♥.