2014-07-08 72 views
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); 
} 
+0

嗯,我得到了这么多: – BalintN

+0

我认为有一个仿真器连接问题。我遵循这篇文章(http://www.codeproject.com/Articles/738846/How-to-Solve-issues-of-Internet-not-working-with-w)来修复Hyper-V连接设置。 现在模拟器可以看到互联网。 但是,仿真器无法访问主机上的开发Web服务器:我启动在VStudio中创建的测试网站,在其中设置断点。当我从主机上的IE访问它时,我打到了断点。当我尝试从仿真器到达它时,我不会触及断点,也不会从测试网页获得响应... – BalintN

回答

0

OK,看来代码是正确的......

问题是,该手机仿真器无法连接到开发服务器(IIS快递)。我搜索了网页,尝试了一些解决方案,但是我无法做到这一点。在此期间,我抛弃了我的无线连接,不得不删除并重新安装Hyper-V,以使其再次运行。

解决方法是将测试Web应用程序发布到本地IIS(确保它是发布的Debug版本),然后从调试器附加到它,从WP8.1模拟器调用 - 并且工作正常。

相关问题