" URI的格式不能测定"用的WebRequest [英] "The format of the URI could not be determined" with WebRequest

查看:710
本文介绍了" URI的格式不能测定"用的WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行POST使用在C#中的WebRequest的网站。我发布到该网站是一个SMS站点,而MessageText中的URL的一部分。为了避免在URL空间我打电话HttpUtility.Encode()以URL编码它

I'm trying to perform a POST to a site using a WebRequest in C#. The site I'm posting to is an SMS site, and the messagetext is part of the URL. To avoid spaces in the URL I'm calling HttpUtility.Encode() to URL encode it.

不过,我不断收到URIFormatException - 无效的URI:的格式URI无法确定 - 当我使用类似的代码如下:

But I keep getting an URIFormatException - "Invalid URI: The format of the URI could not be determined" - when I use code similar to this:

string url = "http://www.stackoverflow.com?question=a sentence with spaces";
string encoded = HttpUtility.UrlEncode(url);

WebRequest r = WebRequest.Create(encoded);
r.Method = "POST";
r.ContentLength = encoded.Length;
WebResponse response = r.GetResponse();



在发生异常时,我打电话WebRequest.Create()。

The exception occurs when I call WebRequest.Create().

我在做什么错了?

推荐答案

您应该只编码参数,而不是整个URL,这样尝试:

You should only encode the argument, not the entire url, so try:

string url = "http://www.stackoverflow.com?question=" + HttpUtility.UrlEncode("a sentence with spaces");

WebRequest r = WebRequest.Create(url);
r.Method = "POST";
r.ContentLength = encoded.Length;
WebResponse response = r.GetResponse();



编码整个URL将意味着://和?得到编码了。该编码字符串然后不再是一个有效的URL。

Encoding the entire url would mean the :// and the ? get encoded too. The encoded string is then no longer a valid url.

这篇关于" URI的格式不能测定"用的WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆