【问题标题】:411 - Length Required When Attempting to POST411 - 尝试 POST 时需要长度
【发布时间】:2016-10-21 18:13:40
【问题描述】:

我正在使用 Xamarin 开发移动应用程序。这使得我无法调用 webRequest.ContentLength = 0。

这是我尝试发布的方式:

客户来电:

await new AssetEndpoint().UpdateStatus(Authentication, CurrentAsset, ApprovalStatuses[0]);

AssetEndpoint.UpdateStatus:

public Task UpdateStatus(Authentication auth, Asset asset, ApprovalStatus newStatus)
{
    return PostResponseAsync(auth, string.Format(
        ApiUpdateStatus, asset.UID, newStatus.Id));
}

Endpoint.PostResponseAsync:

protected async Task<string> PostResponseAsync(Authentication auth, string apiCall)
{
    var request = WebRequest.Create(string.Concat(BaseUriPath, apiCall)) as HttpWebRequest;
    request.ContentType = "application/json";
    request.Method = method;
    request.Headers["Authorization"] = string.Concat("bearer ", auth.Token.Value);
    var response = await request.GetResponseAsync().ConfigureAwait(false);
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        return await reader.ReadToEndAsync();
    }
}

所以我会修复这个错误吗?我似乎无法弄清楚如何设置内容长度。

【问题讨论】:

  • 请出示CreateRequest方法的代码
  • @MatiasCicero 我已更新帖子以包含 CreateRequest 代码。我把代码拆开,放在 PostResponseAsync 方法中,这样更容易阅读。
  • @kformeck 你没有发布任何东西...你在哪里写入请求流?顺便说一句:一个奇怪的名字 PostResponseAsync
  • 这对你有用吗? request.Content = new ByteArrayContent(content); request.Content.Headers.ContentLength = content.Length;
  • @LukePothier,不,我也无法设置 request.Content。 Xamarin 已经取消了 .NET 的一些功能,以便我相信移动设备可以保持小型化。所以有些事情不能在 Xamarin 中完成,而可以在常规的旧 .NET 中完成

标签: c# .net http mobile xamarin


【解决方案1】:

这可能是您使用的 Xamarin 版本的问题:

http://forums.xamarin.com/discussion/comment/58076#Comment_58076

【讨论】:

    【解决方案2】:
    public class RestClientTest
    {
        public static async Task<string> Login()
        {
            try
            {
                var request = WebRequest.CreateHttp(path);
                request.Headers["Username"] = "xxxxxxxxx";
                request.Headers["Password"] = "xxxxxxxxxxx";
                request.ContentType = "application/json";
                request.Method = "POST";
                byte[] byteArray = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
                using (Stream dataStream = await request.GetRequestStreamAsync())
                {
                    await dataStream.WriteAsync(byteArray, 0, byteArray.Length);
                }
                var response = await request.GetResponseAsync().ConfigureAwait(false);
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    string resp = await reader.ReadToEndAsync();
                    return resp;
                }
            }
            catch (Exception ex)
            {
                return "Error";
            }
        }
    }
    

    如果这对您不起作用,如果您想尝试,我可以提供 HttpClient 示例。在不知道您发布的内容的情况下,我无法提供更多帮助。我还测试了这段代码,没有在正文中发送任何数据,它也可以工作。

    【讨论】:

    • 我不明白。 -2 票,没有任何 cmets。我会停止帮助别人。
    • 自上周以来我没有看过这个问题,所以我不是一个反对的人。我最终以不同的方式解决了这个问题;通过使用 HttpClient 而不是 WebClient。这是一个更容易的方法。但是,我会接受您发布的答案,因为在查看之后,它似乎应该可以正常工作。不幸的是,我没有时间测试它。
    • @kformeck 谢谢。我测试了已发布的 WebRequest 和 HttpClient。两者都有效。
    • 谢谢尤里,我喜欢你的回答。它应该适用于大多数(如果不是全部)场景。在 Xamarin 中开发略有不同,只有 .NET 框架的子集可用,这可以让 .NET 开发人员跳出框框思考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多