如何更改WebClient请求的动词?似乎仅允许/默认为POST,即使在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;
        }

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

最佳答案

如果使用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 ...)

关于c# - 如何在WebClient请求中使用动词GET?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17415709/

10-17 02:01