0
httpwebrequest에서 httpclient로 일부 메소드를 변경하려고합니다. 나는 대부분의 작업을했지만이 작업에 매달렸다. 누군가가 이것을 성취 할 수 있습니까?HttpWebRequest에서 HttpClient로 전환
string url = someurl;
HttpWebRequest request = CreateRequest(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
string body = @"somestring here.";
byte[] postBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
response = (HttpWebResponse)request.GetResponse();
이 방법을 HttpClient를 사용하여 변환해야합니다.
이것은 내가 시도한 것입니다.
string url = someurl;
var client = new HttpClient();;
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));//ACCEPT header
//request.ContentType = "application/x-www-form-urlencoded";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,url);
string body = @"somestring here...";
var content = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
request.Content = content;
var ss = client.PostAsync(url,content).Result;
string str2 = await ss.Content.ReadAsStringAsync();
이 부분이 작동하지 않습니다.
string body = @"somestring here.";
byte[] postBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
아마도 'PostAsync'가 필요할 것 같습니다. 문제의 원인이되는 부분에 대해 자세히 알려주십시오. (우리가'CreateRequest'가 무엇을하는지 모른다는 것을 돕지 않습니다.) –
'HttpClient'는 비동기 메소드만을 가지고 있습니다. 비동기 호출 (.Result, .Wait() ..)을 차단할 필요가없는 코드 비동기입니까? – Crowcoder
지금까지 시도한 것은 무엇입니까? 이 사이트는 코드 변환기가 아닙니다. – Toshi