UploadString期间System.Net.WebClient不引发错误无效的URL? [英] System.Net.WebClient not throwing error during UploadString to invalid url?
问题描述
鉴于这种code,我期待一个例外的情况发生,但 System.Net .WebClient.UploadString 返回一个空字符串转化结果
。
Given this code, I expected an exception to happen, but System.Net.WebClient.UploadString returns an empty string into result
.
我oviously忽视的东西,但什么?
I'm oviously overlooking something, but what?
using System.Net;
using System.Text;
[TestClass()]
public class WebClientTests
{
[TestMethod()]
public void WebClientUploadStringToInvalidUrlTest()
{
var webClient = new WebClient { Encoding = Encoding.UTF8 };
var result = webClient.UploadString("{{foo-bar}}", "snafu");
Assert.IsTrue(string.IsNullOrEmpty(result));
}
[TestMethod()]
[ExpectedException(typeof(ArgumentNullException))]
public void WebClientUploadStringToNullUrlTest()
{
var webClient = new WebClient { Encoding = Encoding.UTF8 };
string url = null;
var result = webClient.UploadString(url, "snafu");
Assert.IsTrue(string.IsNullOrEmpty(result));
}
}
修改的:作为<一个href="http://stackoverflow.com/questions/24082706/system-net-webclient-not-throwing-error-during-uploadstring-to-invalid-url?noredirect=1#comment37140208_24082706">per建议由汉诺增加了一个空
测试以及的,而这将引发 ArgumentNullException
样的,我的预期。
edit: as per suggestion by hanno added a null
test as well, and this throws an ArgumentNullException
which I kind of expected.
推荐答案
这只能通过查看内部的实现细节,回答这个问题。
It is only possible to answer this by looking at internal implementation details.
在preparation的 WebClient的类尝试建立一个真正的从字符串你在超负荷<提供的乌里 HREF =http://msdn.microsoft.com/en-us/library/ms144236相对=nofollow> UploadString 。
In preparation the WebClient class tries to build an real Uri from the string you provided in that overload to UploadString.
在code大致是这样的,在4.0框架:
The code roughly looks like this in the 4.0 framework:
if (!Uri.TryCreate(path, UriKind.Absolute, out address))
{
return new Uri(Path.GetFullPath(path));
}
借助 TryCreate 回报的虚假用于非的相对= nofollow的> 的URI路径一致,因此回落到创建一个乌里
的的你的字符串,返回的Path.GetFullPath 的乌里
与<一个HREF =https://en.wikipedia.org/wiki/File_URI_scheme相对=nofollow>文件://方案
The TryCreate returns false for your non URI conforming path so it falls back to creating an Uri
for the Path.GetFullPath of your string, which returns an Uri
with a file:// scheme.
内部的 Web客户端
使用的WebRequest .Create ,以获得的WebRequest ,能够处理给定的方案。在你的情况下, FileWebRequest 将返回(因方案是文件://
)
Internally the WebClient
uses WebRequest.Create to obtain a WebRequest that is capable of handling the given scheme. In your case a FileWebRequest will be returned (due to the scheme being file://
)
剥离后,以下简化调用序列执行乌里
获得所有的检查和内部管道:
Stripping all checks and internal plumbing the following simplified call sequence is executed after the Uri
is obtained:
var fwr = (FileWebRequest) WebRequest.Create(new Uri(Path.GetFullPath(path)));
fwr.Method ="POST";
var us = fwr.GetRequestStream();
var upload = Encoding.UTF8.GetBytes("snafu");
us.Write(upload,0,upload.Length);
us.Close();
var sr = new StreamReader(fwr.GetResponse().GetResponseStream());
return sr.ReadToEnd();
这并不抛出异常,并返回一个空字符串。
Which doesn't throw an exception and returns an empty string.
您设想的我希望这引发异常,的是错误的。
Your assumption "I expect this to throw an exception" is wrong.
这篇关于UploadString期间System.Net.WebClient不引发错误无效的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!