我对所有这些技术都有些陌生,所以我会尽量保持清楚。
我正在编写一个Windows Phone应用程序,该应用程序将字符串格式的数据发送到服务器:
public class sendDataControl
{
private string response = "";
public void sendToServer(string FullSTR)
{
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://pricequeryserver.azurewebsites.net/api/ReceiptDataService/?incomingdata=");
webClient.UploadStringAsync(uri,FullSTR);
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
}
catch (Exception ex)
...
...
}
}
void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error != null)
{
responseXml=e.Error.Message;
MessageBox.Show(responseXml);
return;
}
else
{
responseXml = e.Result;
}
}
}
该服务器是基本的MVC4,带有我添加的api控制器,需要获取从移动设备发送的数据。
作为测试,我只是取回发送的字符串:
public class ReceiptDataServiceController : ApiController
{
private ReceiptContext db = new ReceiptContext();
...
...
public string GetDataFromMobile(string IncomingData)
{
return IncomingData;
}
}
运行应用程序时,我通过responseXml收到错误消息:“远程服务器返回错误:NotFound”。
服务器在IIS和azure上通过各种浏览器返回正确的答案,而在移动仿真器中则返回正确的答案。
有什么建议?
如果查看一下正在使用的UploadStringAsync重载的文档,您会注意到它使用POST
method发送数据。在控制器中时,您仅实现了GET
。为了你
您必须使用UploadStringAsync的其他重载,它允许您指定要使用的HTTP VERB。并且您必须指定GET
。您的客户代码应转换为:
webClient.UploadStringAsync(uri,"GET", FullSTR);
对于GET
像您这样的简单操作,最好的解决方案是实际使用DownloadStringAsync:
var fullUri = new Uri("http://pricequeryserver.azurewebsites.net/api/ReceiptDataService/?incomingdata=" + FullStr);
webClient.DownloadStringAsync(fullUri);
无论如何,您的问题与Windows Azure无关,因此与已删除的标签无关。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句