简体   繁体   English

System.Net.WebClient在UploadString期间没有将错误丢失到无效的url?

[英]System.Net.WebClient not throwing error during UploadString to invalid url?

Given this code, I expected an exception to happen, but System.Net.WebClient.UploadString returns an empty string into result . 鉴于此代码,我预计会发生异常,但System.Net.WebClient.UploadString会将空字符串返回到result

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));
    }

}

edit : as per suggestion by hanno added a null test as well, and this throws an ArgumentNullException which I kind of expected. 编辑 :根据hanno的 建议也添加了一个null测试,这会抛出一个我所期望的ArgumentNullException

It is only possible to answer this by looking at internal implementation details. 只有通过查看内部实现细节才能回答这个问题。

In preparation the WebClient class tries to build an real Uri from the string you provided in that overload to UploadString . 在准备过程中, WebClient类尝试从您在UploadString的重载中提供的字符串构建一个真正的Uri

The code roughly looks like this in the 4.0 framework: 代码在4.0框架中大致如下所示:

 if (!Uri.TryCreate(path, UriKind.Absolute, out address))
 {
      return new Uri(Path.GetFullPath(path));
 } 

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 . TryCreate为非URI符合路径返回false ,因此它回退到为字符串的Path.GetFullPath创建一个Uri ,它返回一个带有file://方案Uri

Internally the WebClient uses WebRequest.Create to obtain a WebRequest that is capable of handling the given scheme . 在内部, WebClient使用WebRequest.Create来获取能够处理给定方案WebRequest In your case a FileWebRequest will be returned (due to the scheme being file:// ) 在您的情况下,将返回FileWebRequest (由于该方案为file://

Stripping all checks and internal plumbing the following simplified call sequence is executed after the Uri is obtained: 剥离所有检查和内部管道,在获得Uri后执行以下简化的调用序列:

        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. 你的假设“我希望这会引发异常”是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM