带有unicode url的httpWebRequest Referer异常

时间:2013-03-26 11:12:33

标签: c# httpwebrequest http-referer

我有一些代码使用httpWebRequest来发布数据,但是当我在Referer中使用unicode文本时,它总是显示异常:“指定的值具有无效的控制字符。”

            CookieContainer cookieContainer = new CookieContainer();
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Headers["Accept-Encoding"] = "gzip, deflate";
            httpWebRequest.AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate);
            httpWebRequest.Timeout = this.Timeout;
            httpWebRequest.CookieContainer = cookieContainer;
            httpWebRequest.Method = method;
            httpWebRequest.KeepAlive = this.KeepAlive;
            httpWebRequest.ContentType = this.ContentType;
            httpWebRequest.Accept = this.Accept;
            httpWebRequest.UserAgent = this.UserAgent;
            httpWebRequest.Referer = this.Referer;
            httpWebRequest.ProtocolVersion = HttpVersion.Version11;
            httpWebRequest.AllowAutoRedirect = true;
            httpWebRequest.ServicePoint.Expect100Continue = false;
            httpWebRequest.AllowWriteStreamBuffering = false;
            httpWebRequest.Credentials =  CredentialCache.DefaultCredentials;
            httpWebRequest.Proxy = WebRequest.DefaultWebProxy;

网址为:somewhere.com/index?1yyezksvrgzkelork□=rgu{z5iihy5zvreiihy&1iutlomLork=gjsot5otjk~ 推荐人:somewhere.com/index?1yyezksvrgzkelork□=rgu{z5iihy5zvreiihy&1iutlomLork=gjsot5otjk~ 如果只使用url它正常运行。

1 个答案:

答案 0 :(得分:1)

尝试使用HttpUtility.UrlEncode(...)

对您的引用进行编码