0

我正在寻找将带有JSON类似参数的请求发送到服务器的解决方案。

我用这个代码,

var httpClient = new HttpClient();
var tempByteArray = Encoding.UTF8.GetBytes("my valid json");
var stream = new MemoryStream(tempByteArray);
var streamContent = new StreamContent(stream);
var request = new HttpRequestMessage(HttpMethod.Post, Constants.LocalServer);
request.Content = streamContent;
request.Headers.TransferEncodingChunked = true;
HttpResponseMessage response = await httpClient.SendAsync(request);

但作为回应,我得到:

{
    StatusCode: 501,
    ReasonPhrase: 'NotImplemented',
    Version: 1.0,
    Content: System.Net.Http.StreamContent,
    Headers: {
        X-Squid-Error: ERR_UNSUP_REQ0X-Cache: MISSfromproxy3.itos.orgX-Cache-Lookup: NONEfromproxy3.companyname.org: portProxy-Connection: closeDate: Thu,
        18Apr201309: 17: 53GMTServer: squid/2.6.STABLE21Via: 1.0proxy3.companyname.org: port(squid/2.6.STABLE21)Content-Length: 1099Content-Type: text/htmlExpires: Thu,
        18Apr201309: 17: 53GMT
    }
}

可能有另一种方式在 Win8 上使用 json 参数发送请求?

更新我找到了解决方案:

public static async Task<string> LoadData(string json, string serverUrl)  
{
    var request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.LocalServer));
    request.ContentType = "application/json";
    request.Method = "POST";

    using (var requestStream = await request.GetRequestStreamAsync())
    {
        var writer = new StreamWriter(requestStream);
        writer.Write(json);
        writer.Flush();
    }

    using (var resp = await request.GetResponseAsync())
    {
        using (var responseStream = resp.GetResponseStream())
        {
            var reader = new StreamReader(responseStream);
            return  = reader.ReadToEnd();
        }
    }
}

它工作得很好,但必须存在更简单的方式(我希望)。我会试着找到它。

4

2 回答 2

0

当我使用您的两个代码片段发布数据时,我发现请求中存在一些差异。

这是第一个代码示例的原始帖子(您说它不起作用):

POST http://testing.foo.com/api/Values HTTP/1.1
Host: testing.foo.com
Expect: 100-continue
Connection: Keep-Alive
Content-Length: 75

{
id:"1",
title:"title text",
post:"post text",
isDeleted:"False"
}

这是您更新中代码的原始帖子(您说有效的代码):

POST http://testing.foo.com/api/Values HTTP/1.1
Content-Type: application/json
Host: testing.foo.com
Content-Length: 75
Expect: 100-continue

{
id:"2",
title:"title text",
post:"post text",
isDeleted:"False"
}

两种请求的区别如下:

  1. 在第一个请求中,从不设置内容类型。
  2. 在第一个请求中,内容是 UTF8 编码的。

要修复您的非工作代码,我建议您尝试以下一项或两项:

  1. 将内容类型设置为 application/json
  2. 不是 UTF8 编码请求
于 2013-04-18T18:13:04.077 回答
0

在那个时候,这个解决方案是最有用的。

public static async Task<string> LoadData(string json, string serverUrl)  
{
    var request = (HttpWebRequest)WebRequest.Create(new Uri(Constants.LocalServer));
    request.ContentType = "application/json";
    request.Method = "POST";

    using (var requestStream = await request.GetRequestStreamAsync())
    {
        var writer = new StreamWriter(requestStream);
        writer.Write(json);
        writer.Flush();
    }

    using (var resp = await request.GetResponseAsync())
    {
        using (var responseStream = resp.GetResponseStream())
        {
            var reader = new StreamReader(responseStream);
            return  = reader.ReadToEnd();
        }
    }
}
于 2013-04-22T08:28:30.360 回答