简体   繁体   English

如何在Windows Phone 8中将数据发布到JSON Web服务上

[英]How to post data on JSON web services in Windows Phone 8

I want to post data on JSON web services for login credentials for user. 我想在JSON Web服务上发布数据以获取用户的登录凭据。

I use the below code to post data on JSON web service. 我使用以下代码将数据发布到JSON Web服务上。

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://test/TestService/Service.svc/json/Login");

        request.ContentType = "application/json";
        //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 = "{'userid': '" + textUserid.Text + "','password':'" + textPassword.Text + "'}";

            // 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;
            MessageBox.Show("Your Response: " + read);
            // Close the stream object
            streamResponse.Close();
            streamRead.Close();
            response.Close();
    } 

and I import following namespace in my code 然后在代码中导入以下名称空间

using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Text;

I am calling Button_Click_1 method clicking login button from my Windows Phone simulator. 我正在从Windows Phone模拟器中调用单击登录按钮的Button_Click_1方法。

But I am getting this error: 但我收到此错误:

Error   1   'System.Net.WebRequest' does not contain a definition for 'GetRequestStream' and no extension method 'GetRequestStream' accepting a first argument of type 'System.Net.WebRequest' could be found (are you missing a using directive or an assembly reference?) E:\Users\maan\Documents\Visual Studio 2012\Projects\TestWebservice\TestWebservice\MainPage.xaml.cs  99  39  TestWebservice

and

Error   2   'System.Net.WebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.WebRequest' could be found (are you missing a using directive or an assembly reference?)   E:\Users\maan\Documents\Visual Studio 2012\Projects\TestWebservice\TestWebservice\MainPage.xaml.cs  106 39  TestWebservice

Please help me I am new to developing Windows mobile application. 请帮助我,我是开发Windows移动应用程序的新手。

Please check the below code, i hope it will help you: 请检查以下代码,希望对您有所帮助:

void sendRequest()
{
   Uri myUri = new Uri(http://www.yourwebsite.com);
   HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
   myRequest.Method = AppResources.POST;
   myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}

void GetRequestStreamCallback(IAsyncResult callbackResult)
{
    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;

    // End the stream request operation
    Stream postStream = myRequest.EndGetRequestStream(callbackResult);

    // Create the post data
    string postData = "INSERT HERE THE JASON YOU WANT TO SEND";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // Add the post data to the web request
    postStream.Write(byteArray, 0, byteArray.Length);
    postStream.Close();

    // Start the web request
    myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}

 void GetResponsetStreamCallback(IAsyncResult callbackResult)
 {
     lib = new ApiLibrary();

     try
     {
         HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
         HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
         string result = "";
         using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
         {
             result = httpWebStreamReader.ReadToEnd();
         }

            string APIResult = result;

         }
         catch (Exception e)
         {

      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM