C# 如何在 WebClient 请求中使用动词 GET?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17415709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:26:13  来源:igfitidea点击:

How to use verb GET with WebClient request?

c#webclienthttp-get

提问by FlavorScape

How might I change the verb of a WebClient request? It seems to only allow/default to POST, even in the case of DownloadString.

如何更改 WebClient 请求的动词?即使在下载字符串的情况下,它似乎也只允许/默认为 POST。

        try
        {
            WebClient client = new WebClient();               
            client.QueryString.Add("apiKey", TRANSCODE_KEY);
            client.QueryString.Add("taskId", taskId);
            string response = client.DownloadString(TRANSCODE_URI + "task");                
            result = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(response);
        }
        catch (Exception ex )
        {
            result = null;
            error = ex.Message + " " + ex.InnerException;
        }

And Fiddler says:

Fiddler 说:

POST http://someservice?apikey=20130701-234126753-X7384&taskId=20130701-234126753-258877330210884 HTTP/1.1
Content-Length: 0

采纳答案by Niklas Bjorkman

If you use HttpWebRequest instead you would get more control of the call. You can change the REST verb by the Method property (default is GET)

如果您改用 HttpWebRequest,您将获得对调用的更多控制。您可以通过 Method 属性更改 REST 动词(默认为 GET)

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(HostURI);
request.Method = "GET";
String test = String.Empty;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    test = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
 }
 DeserializeObject(test ...)

回答by jeffo

Not sure if you can use WebClient for that. But why not use HttpClient.GetAsync Method (String) http://msdn.microsoft.com/en-us/library/hh158944.aspx

不确定您是否可以为此使用 WebClient。但为什么不使用 HttpClient.GetAsync 方法(字符串)http://msdn.microsoft.com/en-us/library/hh158944.aspx

回答by Niklas Bjorkman

As one can see in the source code of .NET, the HTTP Method of the DownloadStringdepends on the state of the private WebClient instance field m_Method, which is cleared to null upon each new request method call (link) and defaults to the Web request Creator (depends on the URI, for example ftp protocol gets another creator), but this is not thread safe.

正如在 .NET 的源代码中看到的,DownloadString的 HTTP Method依赖于私有 WebClient 实例字段 m_Method 的状态,该字段在每次新的请求方法调用(link)时被清除为 null并默认为 Web 请求创建者(取决于 URI,例如 ftp 协议获取另一个创建者),但这不是线程安全的。

Perhaps you are sharing this WebClient instance among several calls simultaneously?

也许您正在同时在多个调用之间共享这个 WebClient 实例?

So it gets confused. Either this or the URI confuses the WebRequest creator.

所以会混淆。这个或 URI 混淆了 WebRequest 创建者。