411-尝试POST时所需的长度

本文关键字:尝试 POST 411- | 更新日期: 2023-09-27 18:14:09

我正在使用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();
    }
}

所以我会着手修复这个错误吗?我似乎不知道如何设置内容长度。

411-尝试POST时所需的长度

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

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

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示例。不知道你在发什么,我帮不了更多的忙。我还测试了这段代码,没有在正文中发送任何数据,它也能工作。