Windows Phone 8 앱에서 POST 메서드를 사용하여 RESTfull 서비스를 호출하고 싶습니다. 따라서 JSON으로 파싱 한 후 요청 본문에 보내려는 데이터를 삽입해야합니다. 이렇게하려면 다음 코드를 사용했습니다 :Windows Phone에서 작업 스레드의 UI 스레드에서 값을 가져 오는 방법?
enter cprivate void NextArrow_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (!String.IsNullOrEmpty(TxtBox_mail.Text))
{
Uri myUri = new Uri("http://myUri");
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/json";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
}
public void GetRequestStreamCallback(IAsyncResult callbackResult)
{
byte[] byteArray = null;
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
// Create the post data
Dispatcher.BeginInvoke(() =>
{
string mailToCheck = TxtBox_mail.Text.ToString();
string postData = JsonConvert.SerializeObject(mailToCheck);
byteArray = Encoding.UTF8.GetBytes(postData);
});
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}
나는 UI 스레드에 텍스트 상자 컨트롤의 값을 얻기 위해 디스패처를 사용했습니다하지만 BYTEARRAY 항상 널입니다. 누군가 여기서 무엇이 잘못 될 수 있는지 알고 있습니까? 미리 감사드립니다.
'byteArray = Encoding.UTF8.GetBytes (postData);'postData 란 무엇입니까? 기대하는 문자열이 포함되어 있습니까? 'Debug.WriteLine()'을 사용하여 값을 출력하십시오. – Sjips