如何将windows8应用程序c#与php api连接

如何将windows8应用程序c#与php api连接,c#,php,json,api,windows-8,C#,Php,Json,Api,Windows 8,我有一段代码,其中我正在尝试连接到windows8应用程序的php基本api服务器。但是,我不知道如果我尝试调试它,url是正确的,并且变量已设置,因此没有得到任何结果。 我是windows8应用程序和c#的新手,经过几次研究,这就是连接到api服务器的样子 需要帮忙吗 您可以在一个页面中使用PHP构建API,该页面通过GET或POST接收信息,并返回JSON对象或XML(有关您想要的函数,请参见) 之后,您可以通过简单的HTTP请求将其用于应用程序。API与语言无关。调用API时,您可以读取

我有一段代码,其中我正在尝试连接到windows8应用程序的php基本api服务器。但是,我不知道如果我尝试调试它,url是正确的,并且变量已设置,因此没有得到任何结果。 我是windows8应用程序和c#的新手,经过几次研究,这就是连接到api服务器的样子 需要帮忙吗



您可以在一个页面中使用PHP构建API,该页面通过GET或POST接收信息,并返回JSON对象或XML(有关您想要的函数,请参见)


之后,您可以通过简单的HTTP请求将其用于应用程序。

API与语言无关。调用API时,您可以读取json或xml或任何其他格式的响应,而不管使用何种语言编写响应。@我是windows8应用程序的新手,查看我的代码时,我希望服务器会提供json格式的响应。我缺少什么?首先,你有没有检查你的API是否以指定的格式响应良好?是的,我们检查了,它正在响应。实际上它也在不同的移动平台(ios和android)上工作@GunsIf如果我是你,我会直接在浏览器中调试API url,看看它是否返回什么。所以,也许你应该直接在浏览器中调用url,或者使用method=post的表单制作html,然后在浏览器中运行它并检查它返回的内容。我已经尝试过这个方法,但我需要更具体的信息来说明我的情况
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();
        }

    }