UploadString期间System.Net.WebClient不引发错误无效的URL?错误、System、UploadString、Net

由网友(緈鍢de承諾)分享简介:鉴于这种code,我期待一个例外的情况发生,但 System.Net .WebClient.UploadString 返回一个空字符串转化结果。Given this code, I expected an exception to happen, but System.Net.WebClient.UploadStri...

鉴于这种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));
    }

}

修改的:作为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的类尝试建立一个真正的从字符串你在超负荷 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 回报的虚假用于非的 的URI路径一致,因此回落到创建一个乌里的的你的字符串,返回的Path.GetFullPath 的乌里与文件://方案

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.

阅读全文

相关推荐

最新文章