【问题标题】:Calling web service method in Windows Phone 8 App在 Windows Phone 8 App 中调用 Web 服务方法
【发布时间】:2013-10-09 09:52:41
【问题描述】:

我有一个托管在与 SQL Server 交互的服务上的 Web 服务。 我必须开发一个 Windows Phone 8 应用程序,该应用程序应该与该 Web 服务交互以从服务器获取数据。 我正在使用 webclient 但得到响应:远程服务器返回一个错误 notfound 。 我不知道如何调用方法.. 哪个更好 HTTP客户端 网页客户端 或任何其他方法

【问题讨论】:

    标签: c# windows-phone webclient dotnet-httpclient


    【解决方案1】:
    public ConstructoreName()
        {
            InitializeComponent();
    
            ServiceReferenceCustomer.OfferhutCustomerClient ohCustomer = new ServiceReferenceCustomer.OfferhutCustomerClient();
    
            ohCustomer.getOfferAsync(3); //here getOffer is a method and 3 is a parameter
            ohCustomer.getOfferCompleted += new EventHandler<getOfferCompletedEventArgs>(getOffer_completed);
        }
    
    void getOffer_completed(object sender, getOfferCompletedEventArgs e)
        {
            ServiceReferenceCustomer.offer res;
            res = e.Result;
    
            offerTitle.Text = res.title;
            offerFirstPara.Text = res.shopName + " \n" + res.title + " \n" + res.date;
            offerSecendPara.Text = res.description;
        }
    

    我认为这对你有帮助..

    【讨论】:

      【解决方案2】:

      我认为这应该对你有所帮助:

      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
          // Create a new HttpWebRequest object.
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/webservicelogin/webservice.asmx/ReadTotalOutstandingInvoice");
      
          request.ContentType = "application/x-www-form-urlencoded";
          request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)";
          request.CookieContainer = cookie;
      
          // Set the Method property to 'POST' to post data to the URI.
          request.Method = "POST";
      
          // start the asynchronous operation
          request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
      
      }
      
      private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
      {
          HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
      
          // End the operation
          Stream postStream = request.EndGetRequestStream(asynchronousResult);
      
          //postData value
          string postData = "xxxxxxxxxx";  
      
          // Convert the string into a byte array. 
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
      
          // Write to the request stream.
          postStream.Write(byteArray, 0, postData.Length);
          postStream.Close();
      
          // Start the asynchronous operation to get the response
          request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
      
      }
      
      private void GetResponseCallback(IAsyncResult asynchronousResult)
      {
      
                  HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                  // End the operation
      
                  HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
      
                  Stream streamResponse = response.GetResponseStream();
      
                  StreamReader streamRead = new StreamReader(streamResponse);
                  string read = streamRead.ReadToEnd();
      
                  //respond from httpRequest
                  TextBox.Text = read;
      
                  // Close the stream object
                  streamResponse.Close();
                  streamRead.Close();
                  response.Close();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多