Xamarin Forms - 使用Visual Studio 2012编写的Web服务

时间:2014-12-02 20:10:23

标签: web-services xamarin

我在Visual Studio 2012中编写了一个简单的Web服务。

当我在Internet Explorer中测试Web服务时,我得到以下结果。

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">[{"ID":1,"ConsultantName":"Jim",
"ConsultantEmailAddress":"Jim@xyz.com","ConsultantCellNumber":null,
"ConsultantPhoneNumber":null},{"ID":2,"ConsultantName":"Steve","ConsultantEmailAddress":"Steve@xyz.com",
"ConsultantCellNumber":null,"ConsultantPhoneNumber":null}]</string>

以JSON格式返回的两条记录

一切都很好

现在在XAMARIN中调用Web服务:

  1. 经过大量研究后我发现“LOCALHOST”在ANDROID世界中没有任何意义 - 所以我将“LOCALHOST”替换为10.0.2.2并且执行了更多的代码。
  2. 我查看了在使用Web服务时可以找到的所有示例,并最终确定了以下修改后的代码:
  3. 在下面的代码中,似乎在调用Web服务时请求成功,但是当代码尝试使用结果(JSON字符串)时,会发生400 Bad Request错误。

    这是发生错误的行:

    HttpWebResponse response =(HttpWebResponse)request.EndGetResponse(callbackResult);

    (此行位于所包含代码的底部)

    错误 - //远程服务器返回错误:(400)错误请求

    代码如下: 如果您有任何意见或指导,我将不胜感激。

    除此之外,如果XAMARIN能够提供一个消费Web服务的真实简单示例,那将会很棒。到目前为止,我见过的每个例子都有一些复杂问题,如果你的知识很少,你甚至可以在理解代码之前做大量的研究。 如何使用非常简单的代码覆盖视频:使用XAMARIN Forms进行异步Web消费。 我面临的问题是我花了三天时间摆弄。如果我有一个简单的工作示例,我可以慢慢增加复杂性,直到我完全理解发生了什么,但现在我仍然不知道我的问题在哪里。

    感谢您的期待。

        private async Task ExecuteLoadConsultants ()
        {
    
            Initialized = true;
            IsBusy = true;
    
            ConsultantInfo x = new ConsultantInfo{ ConsultantName = "WEwewewewe" };
                Consultants.Add (x);
    
            // call to webservice 
    
                HttpWebRequest webRequest = 
                           (HttpWebRequest)WebRequest.Create ("http://10.0.2.2:53498/TennantMobileWS1.asmx?op=GetConsultantAll");
            webRequest.UseDefaultCredentials = true;
            webRequest.Method = "POST";
            webRequest.Accept = "application/json";
            webRequest.ContentType = "application/json";
            webRequest.UseDefaultCredentials = true;
    
            webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
    
            IsBusy = false;
        }
    
    
    
        private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
    
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            // End the stream request operation
    
            Stream postStream = request.EndGetRequestStream(asynchronousResult);
    
            //Start the web request
            request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
    
        }
    
        void GetResponceStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
    
    
            //  Remote server returned an error: (400) Bad Request on line below
    
    
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();
    
        }
    

1 个答案:

答案 0 :(得分:0)

使用ngrok建立隧道并访问您的本地主机,并为条形码请求执行此操作:

点击“开始”按钮 在搜索框中,输入“打开或关闭Windows功能” 在功能窗口中,单击:“Internet信息服务” 点击:“万维网服务” 单击:“应用程序开发功能” 检查(启用)功能。我检查了除CGI以外的所有内容。

我的表格here

我希望这可以帮助你:)。

归功于相应的