从代码调用Web资源时的常见任务是构建查询字符串以包含所有必需参数.虽然无论如何都没有火箭科学,但是你需要注意一些漂亮的细节,&
如果不是第一个参数,编码参数等.
这样做的代码非常简单,但有点单调乏味:
StringBuilder SB = new StringBuilder();
if (NeedsToAddParameter A)
{
SB.Append("A="); SB.Append(HttpUtility.UrlEncode("TheValueOfA"));
}
if (NeedsToAddParameter B)
{
if (SB.Length>0) SB.Append("&");
SB.Append("B="); SB.Append(HttpUtility.UrlEncode("TheValueOfB")); }
}
Run Code Online (Sandbox Code Playgroud)
这是一个常见的任务,人们期望实用程序类存在,使其更加优雅和可读.扫描MSDN,我找不到一个 - 这让我想到了以下问题:
你知道做上述事情最干净的方式是什么?
我收到了相当自我解释的错误:
从客户端(*)检测到潜在危险的Request.Path值.
问题是由*
请求URL引起的:
https://stackoverflow.com/Search/test*/0/1/10/1
Run Code Online (Sandbox Code Playgroud)
此网址用于填充搜索页面,其中"test*"是搜索字词,其余网址与各种其他过滤条件相关.
是否有一种简单的方法可以在URL中允许这些特殊字符?我试过修改web.config
,但无济于事.
我应该手动编码/解码特殊字符吗?或者有这样做的最佳实践,我想避免使用查询字符串. - 但它可能是一种选择.
应用程序本身是一个c# asp.net
webforms应用程序,它使用路由生成上面的漂亮URL.
我有一个构建URL的Silverlight应用程序.此URL是对基于REST的服务的调用.此服务需要一个表示位置的参数.位置是"城市,州"的形式.要构建此URL,我将调用以下代码:
string url = "http://www.example.com/myService.svc/";
url += HttpUtility.UrlEncode(locationTextBox.Text);
Run Code Online (Sandbox Code Playgroud)
如果用户在locationTextBox中输入"chicago,il",结果如下所示:
http://www.example.com/myService.svc/chicago%2c+il
Run Code Online (Sandbox Code Playgroud)
但实际上,我有点期待网址看起来像;
http://www.example.com/myService.svc/chicago,%20il
Run Code Online (Sandbox Code Playgroud)
通过浏览器URL测试我的服务时,我期待的那个工作.但是,生成的URL无法正常工作.我究竟做错了什么?
这两个函数之间究竟有什么区别.输出看起来很相似,除了Uri.EscapeUriString
编码空格%20
并将Server.UrlEncode
它们编码为+
符号.
最后应该优先使用的问题
我在Windows Mobile上使用紧凑框架/ C#.
在我的应用程序中,我通过序列化对象并使用HttpWebRequest/POST请求将数据上传到服务器来上传数据.在服务器上,后置数据被反序列化并保存到数据库.
前几天我意识到我在帖子数据(&符号等)中遇到了特殊字符的问题.所以我在方法中引入了Uri.EscapeDataString(),一切都很顺利.
但是,今天我发现当应用程序尝试上传大量数据时出现问题(我不确定此刻究竟表示"大"的是什么!)
现有代码(种类)
var uploadData = new List<Things>();
uploadData.Add(new Thing() { Name = "Test 01" });
uploadData.Add(new Thing() { Name = "Test 02" });
uploadData.Add(new Thing() { Name = "Test with an & Ampersand " }); // Do this a lot!!
var postData = "uploadData=" + Uri.EscapeDataString(JsonConvert.SerializeObject(uploadData, new IsoDateTimeConverter()));
Run Code Online (Sandbox Code Playgroud)
问题
对Uri.EscapeDataString()的调用导致以下异常:
System.UriFormatException:无效的URI:Uri字符串太长.
题
有没有其他方法来准备上传数据?
据我所知,HttpUtility(它有自己的Encode/Decode方法)不适用于紧凑的框架.
HttpServerUtility.UrlPathEncode
和之间有什么区别HttpServerUtility.UrlEncode
?什么时候我应该选择另一个?
鉴于以下尝试将数据发布到生成PDF文件的Web服务,PDF rocket(顺便说一句,这很棒).
我收到错误无效的URI:uri字符串太长
为什么有人会对POST
ed数据施加任意限制?
using (var client = new HttpClient())
{
// Build the conversion options
var options = new Dictionary<string, string>
{
{ "value", html },
{ "apikey", ConfigurationManager.AppSettings["pdf:key"] },
{ "MarginLeft", "10" },
{ "MarginRight", "10" }
};
// THIS LINE RAISES THE EXCEPTION
var content = new FormUrlEncodedContent(options);
var response = await client.PostAsync("https://api.html2pdfrocket.com/pdf", content);
var result = await response.Content.ReadAsByteArrayAsync();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个荒谬的错误.
{System.UriFormatException: Invalid URI: The Uri string is …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C#中的WebRequest对站点执行POST.我发布的网站是一个SMS站点,而messagetext是URL的一部分.为了避免URL中的空格,我正在调用HttpUtility.Encode()来对其进行URL编码.
但我不断收到URIFormatException - "无效的URI:无法确定URI的格式" - 当我使用类似于此的代码时:
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();
Run Code Online (Sandbox Code Playgroud)
当我调用WebRequest.Create()时会发生异常.
我究竟做错了什么?
在最近的一个项目中,我有幸排除了一个错误,该错误涉及当文件名中有空格时图片无法加载.我想"这是一个简单的问题,我会的UrlEncode()
!" 但是,NAY!简单地使用UrlEncode()
没有解决问题.
新的问题是HttpUtilities.UrlEncode()
方法交换空间() to plusses (
+
)代替的%20
,如浏览器通缉.因此,file+image+name.jpg
如果file%20image%20name.jpg
发现正确,将返回not-found .
值得庆幸的是,一位同事HttpUtilities.UrlPathEncode()
向我指出哪种用途%20
代替空间+
.
为什么有两种处理Url编码的方法?为什么有两个命令行为如此不同?
根据http://ayende.com/blog/4599/hunt-the-bug,我遇到了其中一种情况,即"在这种情况下无法提供响应".
非常简化,以下内容在Windows Server 2008/IIS7/ASP.NET 4.0的某些情况下引发异常
public class Global : HttpApplication
{
public void Application_Start(object sender, EventArgs e)
{
HttpUtility.UrlEncode("Error inside!");
}
}
Run Code Online (Sandbox Code Playgroud)
我见过的解决方案涉及以下其中一项:
也许这不是我最好的谷歌搜索日,但如何实现HttpEncoder.Default?
建议?
我在表单中有一个原始文本字符串:
http://www.site.com/abc/%3FSubscriptionId%3DAKIAI5N2ARPTHQHTRGMA%26creative%3D165953%26creative%3DB0058RECN6
Run Code Online (Sandbox Code Playgroud)
是否有任何内置的实用程序来消除这些字符?
我正在使用ASP.NET MVC 3
我在发送POST数据时遇到问题,包括密码字段中的"+"字符,
string postData = String.Format("username={0}&password={1}", "anyname", "+13Gt2");
Run Code Online (Sandbox Code Playgroud)
我正在使用HttpWebRequest和webbrowser来查看结果,当我尝试使用HttpWebRequest从我的C#WinForms 登录到POST数据到网站时,它告诉我密码不正确.(在源代码[richTexbox1]和webBrowser1中).尝试使用我的另一个帐户,它不包含'+'字符,它允许我正确登录(使用字节数组并将其写入流)
byte[] byteArray = Encoding.ASCII.GetBytes(postData); //get the data
request.Method = "POST";
request.Accept = "text/html";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream newStream = request.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close(); //this works well if user does not includes symbols
Run Code Online (Sandbox Code Playgroud)
从这个问题我发现这HttpUtility.UrlEncode()
是逃避非法字符的解决方案,但我无法找到如何正确使用它,我的问题是,在对我的POST数据进行url编码后urlEncode()
如何正确地将数据发送到我的请求?
这就是我一直在尝试HOURS让它发挥作用,但没有运气,
第一种方法
string urlEncoded = HttpUtility.UrlEncode(postData, ASCIIEncoding.ASCII);
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = urlEncoded.Length;
StreamWriter wr = new StreamWriter(request.GetRequestStream(),ASCIIEncoding.ASCII);
wr.Write(urlEncoded); …
Run Code Online (Sandbox Code Playgroud) c# ×7
url ×5
asp.net ×4
.net ×3
urlencode ×3
encoding ×2
asp.net-mvc ×1
escaping ×1
exception ×1
post ×1
query-string ×1
routing ×1
silverlight ×1
uri ×1
url-encoding ×1
webforms ×1
winforms ×1