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();
}
}
그래서이 오류를 고칠까요? 콘텐츠 길이를 설정하는 방법을 알 수없는 것 같습니다.
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 샘플을 제공 할 수 있습니다. 당신이 무엇을 게시하는지 모르면 더 이상 도울 수 없습니다. 또한 본문에 데이터를 전송하지 않고이 코드를 테스트했으며 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다