如何发布PUT HttpWebRequest [英] How to issue PUT HttpWebRequest

查看:370
本文介绍了如何发布PUT HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与需要PUT更新数据的API集成:

I'm trying to integrate with an API that requires a PUT to update data:

以下是他们使用curl的例子:

Here's an example from them using curl:

curl --request PUT \
     --user-agent "Your Client Name/1.0" \
     --header "Content-Type: application/xml" \
     --data-binary '<order><status_id>10</status_id></order>' \
     https://www.example.com/api/v2/orders/101

但是,我需要通过.NET MVC 3使用JSON(也支持JSON).

However, I'd need to use JSON (they support that as well) using .NET MVC 3. Any idea on how I can do that?

我将以下代码成功用于GET:

I use the code below for GET successfully:

Order obj = Call<Order>(url, "GET");

    private T Call<T>(string url, string methodType) where T : class {
        T result;
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = methodType;
        request.Accept = "application/json";
        request.ContentType = "application/json";

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            string jsonData = reader.ReadToEnd();
            result = (T)jsSerializer.Deserialize<T>(jsonData);
        }

        return result;
    }

但是,我可以使用类似的方法发布PUT吗?

However, can I issue a PUT using a similar method?

Order obj = Call<Order>(url, "PUT");

如果是这样,我应该将所需的数据放在数据二进制"中?

If so, where do I put the data that's required in "data-binary"?

推荐答案

好吧,这是一个可能的起点-未经测试;直接写入浏览器;不是生产代码;假设PUT调用既发送又接收相同的对象类型(可能不是这种情况)...

Well, here's a possible point of origin - untested; written straight into the browser; not production code; assumes that the PUT call both sends and receives the same object type (which is probably not the case)...

主要的补充是您需要提供请求的ContentLength,并且需要将序列化的JSON对象写入请求流,您可以通过调用HttpWebRequest :: GetRequestStream()获得该对象.这与发布时的方法相同.

The main addition is that you need to supply the request's ContentLength, and you need to write the serialized JSON object to the request stream, which you'll get by calling HttpWebRequest::GetRequestStream(). It's the same approach as when POSTing.

private T Call<T>(string url, string methodType, T data) where T: class
{
    T result;

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = methodType;
    request.ContentType = "application/json";
    request.Accept = "application/json";

    if (methodType == "PUT" || methodType == "POST")
    {
       JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
       string jsonData = jsSerializer.Serialize(data);

       byte[] arrData = Encoding.UTF8.GetBytes(jsonData);
       request.ContentLength = arrData.Length;

       using (Stream dataStream = request.GetRequestStream())
       {
          dataStream.Write(arrData, 0, arrData.Length);
       }
    }

    // Note: You may not need to parse any response content,
    // or it may be a different class
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        using (StreamReader reader 
                          = new StreamReader(response.GetResponseStream()))
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            string jsonData = reader.ReadToEnd();
            result = (T)jsSerializer.Deserialize<T>(jsonData);
        }
    }
    return result;
}

这篇关于如何发布PUT HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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