C# HttpWebRequest GET partially encoded url

筅森魡賤 提交于 2019-12-08 04:02:26

问题


When I send a get using HttpWebRequest is seems to turn it into a uri, run it through an encoder and send the encoded string. When I look at my address in the request after it is created I have the OriginalString which is correct and an AbsoluteUri which is encoded and incorrect. My code and example urls are below.

HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
String responseData = WebResponseGet(webRequest);

OriginalString:"https://api.linkedin.com/v1/people/url=https%3A%2F%2Fwww.linkedin.com%2Fin%2Ffirstmlast"

AbsoluteUri:"https://api.linkedin.com/v1/people/url=https%3A//www.linkedin.com/in/firstmlast"

How can I force HttpWebRequest to send my original string that I passed it and not a uri? Also I cannot send the already encoded string as a query string, LinkedIn requires it to be apart of the url.


回答1:


I found a HackedUri class here: http://blogs.msdn.com/b/xiangfan/archive/2012/01/16/10256915.aspx and created my request like this passing it a "Hacked Uri" instead of a string. This seems to be a security limitation problem with .Net.

HttpWebRequest webRequest = System.Net.WebRequest.Create(HackedUri.Create(url)) as HttpWebRequest;



回答2:


Have you tried double-encoding the relevant part of the URL?

var request = WebRequest.CreateHttp("https://api.linkedin.com/v1/people/url=" + HttpUtility.UrlEncode("https%3A%2F%2Fwww.linkedin.com%2Fin%2Ffirstmlast"));


来源:https://stackoverflow.com/questions/27389003/c-sharp-httpwebrequest-get-partially-encoded-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!