尝试使用 webapi 从 Windows Phone 8 应用程序连接到 ms-Azure 上的 MVC 服务器时出错



我对所有这些技术都有点陌生,所以我会尽量说清楚。

我正在编写一个以字符串格式将数据发送到服务器的 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;
    }   
}

运行应用程序时,我通过响应Xml收到错误:"远程服务器返回错误:未找到"。

服务器从各种浏览器返回正确答案,而在 IIS 和 Azure 上,但不从移动模拟器返回。

有什么建议吗?

如果您查看正在使用的 UploadStringAsync 重载的文档,您会注意到它使用POST方法发送数据。在控制器中,您只实现了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无关,因此删除了标签。

最新更新