0
我有一個網絡服務,預計(unicode UTF-8 encoded
)文本數據作爲HTTP POST
消息。 我想從Windows Phone 8.1 runtime client
使用它。我創建了客戶端,但它沒有調用web URI
。如何通過HttpWebRequest/POST從Windows Phone 8.1發送文本?
這是我在客戶端上使用的代碼:
誰能告訴我缺少什麼?
感謝, 乙
{
...
SendText("http://192.168.1.107:58709/UploadText.aspx"); // The IP belongs to the web server, port is correct. I can invoke it from a browser.
...
}
string StringToSend = "This is a test string uploaded via HTTP POST from WP8";
private void SendText(string Url)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(Url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(UploadText_GetRequestStreamCallback), request);
}
public void UploadText_GetRequestStreamCallback(IAsyncResult asyncResult)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)asyncResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asyncResult);
byte[] postDataAsBytes = System.Text.Encoding.UTF8.GetBytes(StringToSend);
postStream.Write(postDataAsBytes, 0, postDataAsBytes.Length);
postStream.Flush();
// postStream.Close(); // Close is not available in Windows Phone 8.1 runtime project.
// request.ContentLength = postDataAsBytes.ToString(); // request.ContentLength is not available in Windows Phone 8.1 runtime project.
request.Headers["Content-length"] = postDataAsBytes.ToString();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
嗯,我得到了這麼多: – BalintN
我認爲有一個仿真器連接問題。我遵循這篇文章(http://www.codeproject.com/Articles/738846/How-to-Solve-issues-of-Internet-not-working-with-w)來修復Hyper-V連接設置。 現在模擬器可以看到互聯網。 但是,仿真器無法訪問主機上的開發Web服務器:我啓動在VStudio中創建的測試網站,在其中設置斷點。當我從主機上的IE訪問它時,我打到了斷點。當我嘗試從仿真器到達它時,我不會觸及斷點,也不會從測試網頁獲得響應... – BalintN