如何将动词GET与WebClient请求一起使用? [英] How to use verb GET with WebClient request?

查看:108
本文介绍了如何将动词GET与WebClient请求一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改WebClient请求的动词?似乎只允许/默认为POST,即使在DownloadString的情况下.

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

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

提琴手说:

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

推荐答案

如果您使用HttpWebRequest,则可以更好地控制该调用.您可以通过Method属性更改REST动词(默认为GET)

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 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 ...)

这篇关于如何将动词GET与WebClient请求一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆