如何使用php api连接windows8 app c#

Pas*_*bdo 5 php c# api json windows-8

我有这个代码,我试图连接到一个php基础api服务器的Windows8应用程序.但是我没有得到任何结果知道,如果我尝试调试它的网址是正确的,并设置变量.我是windows8应用程序和c#的新手,经过多次研究,这是连接到api服务器看起来像任何帮助请


private void Button_Click(object sender, RoutedEventArgs e)
        {

            var username="lucy";
            var password="lucy";

            var request = HttpWebRequest.Create("http://myURL/login.php?username="+username+"&password="+password) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/json";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the stream request operation

        Stream postStream = request.EndGetRequestStream(asynchronousResult);


        // Create the post data
        string postData = JsonConvert.SerializeObject(postStream).ToString();
        MessageDialog msgDialog1 = new MessageDialog(postData, "bayyanit");
      msgDialog1.ShowAsync();

        Debug.WriteLine(postData);
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);


        postStream.Write(byteArray, 0, byteArray.Length);
      //  postStream.Close();

        //Start the web request
        try
        {
            request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
        }
    catch(Exception ex)
        {
            MessageDialog msgDialog = new MessageDialog(ex.ToString(), "bayyanit");
            msgDialog.ShowAsync();
        }
    }

    void GetResponceStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            MessageDialog msgDialog = new MessageDialog(result, "bayyanit");
            msgDialog.ShowAsync();
        }

    }
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以在单个页面中使用 PHP 构建 API,该页面通过 GET 或 POST 接收信息并返回 JSON 对象或 XML(有关您想要的功能,请参阅php.net ),无论您喜欢什么。

之后,您就可以通过简单的 HTTP 请求将其与您的应用程序一起使用。