나는 꽤 오랜 시간 동안 여기에서 읽었지만이 경우에는 더 이상 익숙하지 않다. 저는 Windows Phone 개발에 익숙하지 않고 다음과 같은 문제에 직면 해 있습니다.C# WP8 async HttpWebRequest XML 요청 메시지로 게시
XML 요청 메시지를 게시해야만 webservice를 호출합니다. 나는 정기적으로 C#에서 작동하는 코드를 가지고
private static string WebRequestPostData(string url, string postData)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.ContentType = "text/xml";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
using (System.Net.WebResponse resp = req.GetResponse())
{
if (resp == null) return null;
using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
{
return sr.ReadToEnd().Trim();
}
}
}
(아래 코드 참조) 그러나 윈도우 폰 (8) 개발이 비동기 할 필요가있다.
문자열 결과는 내가하기 위해 노력하고있어 내 요청 XML 메시지의 값이 보낸private async void DoCallWS()
{
string url = "<my_url>";
// HTTP web request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/xml";
httpWebRequest.Method = "POST";
// Write the request Asynchronously
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
httpWebRequest.EndGetRequestStream, null))
{
string requestXml = "<my_request_xml>";
// convert request to byte array
byte[] requestAsBytes = Encoding.UTF8.GetBytes(requestXml);
// Write the bytes to the stream
await stream.WriteAsync(requestAsBytes , 0, requestAsBytes .Length);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
//return reader.ReadToEnd();
string result = reader.ReadToEnd();
}
}
}
....
: 여기에 주어진 다양한 샘플을 웹을 검색 및 시도 후 나는 다음과 같은 코드에 온비동기 무효 메서드가 선호되지는 않지만 나중에 해결할 것임을 알고 있습니다. 마티아스 샤피로 (http://matthiasshapiro.com/2012/12/10/window-8-win-phone-code-sharing-httpwebrequest-getresponseasync/)에 의해 설명하지만이
올바른 방향 :
Thnx 프랭크
을 정확하게 귀하의 질문은 무엇 다음과 같이
귀하의 방법을 찾아야한다? 사용중인 코드에 어떤 문제가 있습니까? –
웹 서비스에서 답변을 얻지 못했습니다. reader.ReadToEnd()에 내 요청 매개 변수의 값이 있습니다. –
frankwinder