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