如何使用C#将JSON发布到服务器?

时间:2012-02-04 23:46:07

标签: c# json post httpwebrequest

这是我正在使用的代码:

// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";


// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(json);

// this is important - make sure you specify type this way
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
request.ContentLength = postBytes.Length;
request.CookieContainer = Cookies;
request.UserAgent = currentUserAgent;
Stream requestStream = request.GetRequestStream();

// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

// grab te response and print it out to the console along with the status code
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result;
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
    result = rdr.ReadToEnd();
}

return result;

当我运行这个时,我总是得到500个内部服务器错误。

我做错了什么?

13 个答案:

答案 0 :(得分:338)

我这样做的方式是:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{\"user\":\"test\"," +
                  "\"password\":\"bla\"}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

我写了一个库,以更简单的方式执行此任务,它位于:https://github.com/ademargomes/JsonRequest

希望它有所帮助。

答案 1 :(得分:134)

通过利用JavaScriptSerializer的{​​{1}}方法提供对象到JSON的隐式转换,可以改进Ademar的解决方案。

此外,可以利用Serialize语句的默认功能,以省略显式调用usingFlush

Close

答案 2 :(得分:32)

HttpClient类型是WebClientHttpWebRequest的新实现。

您只需使用以下几行。

string myJson = "{'Username': 'myusername','Password':'pass'}";
using (var client = new HttpClient())
{
    var response = await client.PostAsync(
        "http://yourUrl", 
         new StringContent(myJson, Encoding.UTF8, "application/json"));
}

enter image description here

当您多次需要HttpClient时,建议您只创建一个实例并重复使用它或使用新的HttpClientFactory

答案 3 :(得分:31)

除了Sean的帖子之外,没有必要嵌套using语句。通过using StreamWriter,它将在块结束时刷新并关闭,因此无需显式调用Flush()Close()方法:

var request = (HttpWebRequest)WebRequest.Create("http://url");
request.ContentType = "application/json";
request.Method = "POST";

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string json = new JavaScriptSerializer().Serialize(new
                {
                    user = "Foo",
                    password = "Baz"
                });

    streamWriter.Write(json);
}

var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
        var result = streamReader.ReadToEnd();
}

答案 4 :(得分:13)

照顾您正在使用的内容类型:

application/json

来源:

RFC4627

Other post

答案 5 :(得分:12)

如果您需要异步调用,请使用

var request = HttpWebRequest.Create("http://www.maplegraphservices.com/tokkri/webservices/updateProfile.php?oldEmailID=" + App.currentUser.email) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/json";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the stream request operation

        Stream postStream = request.EndGetRequestStream(asynchronousResult);


        // Create the post data
        string postData = JsonConvert.SerializeObject(edit).ToString();

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);


        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        //Start the web request
        request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
    }

    void GetResponceStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            stat.Text = result;
        }

    }

答案 6 :(得分:9)

我最近提出了一种更简单的发布JSON的方法,还有从我的应用中的模型转换的额外步骤。请注意,您必须为控制器创建模型[JsonObject]以获取值并进行转换。

请求

 var model = new MyModel(); 

 using (var client = new HttpClient())
 {
     var uri = new Uri("XXXXXXXXX"); 
     var json = new JavaScriptSerializer().Serialize(model);
     var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
     var response = await Client.PutAsync(uri,stringContent).Result;
     ...
     ...
  }

型号:

[JsonObject]
[Serializable]
public class MyModel
{
    public Decimal Value { get; set; }
    public string Project { get; set; }
    public string FilePath { get; set; }
    public string FileName { get; set; }
}

服务器端:

[HttpPut]     
public async Task<HttpResponseMessage> PutApi([FromBody]MyModel model)
{
    ...
    ... 
}

答案 7 :(得分:6)

This option未提及:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:9000/");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var foo = new User
    {
        user = "Foo",
        password = "Baz"
    }

    await client.PostAsJsonAsync("users/add", foo);
}

答案 8 :(得分:4)

实现这一目标的一些不同和干净的方法是使用像这样的HttpClient:

class Post(ndb.Model):
  modified_date = ndb.DateTimeProperty()

# query
posts = Post.query(
  Post.key.IN(keys),
  Post.modified_date >= start_date,
  Post.modified_date <= end_date).fetch()

答案 9 :(得分:2)

我最终通过包含 .Result

在同步模式下调用
HttpResponseMessage response = null;
try
{
    using (var client = new HttpClient())
    {
       response = client.PostAsync(
        "http://localhost:8000/....",
         new StringContent(myJson,Encoding.UTF8,"application/json")).Result;
    if (response.IsSuccessStatusCode)
        {
            MessageBox.Show("OK");              
        }
        else
        {
            MessageBox.Show("NOK");
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show("ERROR");
}

答案 10 :(得分:2)

我发现这是发布读取的JSON数据的最友好,最简洁的方法:

find . -not -regex ".*/foo/.*" -regex ".*"

我正在使用Microsoft的var url = @"http://www.myapi.com/"; var request = new Request { Greeting = "Hello world!" }; var json = JsonSerializer.Serialize<Request>(request); using (WebClient client = new WebClient()) { var jsonResponse = client.UploadString(url, json); var response = JsonSerializer.Deserialize<Response>(jsonResponse); } 来序列化和反序列化JSON。参见NuGet

答案 11 :(得分:1)

</script> <div class="row"> <div class="large-12 columns"> <p>Übung: Finde das Gegenteil:</p> <table style="width:100%"> <tr> <td>a) schön</td> <td>1 alt</td> </tr> <tr> <td>b) groß</td> <td>2 klein</td> </tr> <tr> <td>c) neu</td> <td>3 langweilig</td> </tr> <tr> <td>d) laut</td> <td>4 leise</td> </tr> <tr> <td>e) interessant</td> <td>5 hässlich</td> </tr> </table> </div> </div> <form action="" method="post"> <div class="row"> <div class="large-12 columns"> <table style="width:100%"> <tr> <td>a)</td> <td> <select name="a"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <div class="message1"></div> </td> </tr> <tr> <td>b)</td> <td> <select name="b"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <div class="message2"></div> </td> </tr> <tr> <td>c)</td> <td> <select name="c"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <div class="message3"></div> </td> </tr> <tr> <td>d)</td> <td> <select name="d"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <div class="message4"></div> </td> </tr> <tr> <td>e)</td> <td> <select name="e"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <div class="message5"></div> </td> </tr> <tr> </table> </div> </div> <div class="row"> <div class="large-12 columns submitting"> <input type="submit" value="Go" class="submit"> </div> </div> </form>

var data = Encoding.ASCII.GetBytes(json);

使用ASCII而不是UFT8

答案 12 :(得分:1)

警告!我对此主题有很强烈的看法。

.NET的现有Web客户端对开发人员不友好! WebRequestWebClient是“如何挫败开发人员”的主要示例。它们冗长而复杂。当您要做的只是在C#中执行一个简单的Post请求。 HttpClient在解决这些问题上有一定的作用,但仍然不足。最重要的是,Microsoft的文档很糟糕……确实很糟糕;除非您要浏览技术性页面和页面。

开放源代码进行救援。有三种出色的开放源代码,免费的NuGet库可供选择。谢天谢地!这些都得到很好的支持和记录,是的,很容易-纠正...超级容易-可以使用。

  • ServiceStack.Text-快速,轻便且富有弹性。
  • RestSharp-简单的REST和HTTP API客户端
  • Flurl-一个流畅,可移植,可测试的HTTP客户端库

它们之间没有太多,但是我会给ServiceStack.Text一点优势……

  • Github星星大致相同。
  • 未解决的问题,以及重要的是,任何问题的解决速度有多快? ServiceStack在这里获得了最快的解决问题且没有未解决的问题的奖项。
  • 文档?所有文档都很好;但是,ServiceStack将其提升到一个新的水平,并以其“文档的黄金标准”而闻名。

好-在ServiceStack.Text中,JSON中的Post Request是什么样的?

var response = "http://example.org/login"
    .PostJsonToUrl(new Login { Username="admin", Password="mypassword" });

那是一行代码。简洁易用!将以上内容与.NET的Http库进行比较。