如何将windows8应用程序c与phpapi连接
本文关键字:phpapi 连接 应用程序 windows8 | 更新日期: 2023-09-27 17:57:47
我在这段代码中尝试连接到windows8应用程序的php基api服务器。但是,我不知道如果我尝试调试它,url是正确的,并且变量是设置好的,所以我没有得到任何结果。我是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();
}
}
您可以在一个页面中使用PHP构建API,该页面通过GET或POST接收信息,并返回JSON对象或XML(有关所需函数,请参阅PHP.net)。
之后,您就可以通过简单的HTTP请求将它与应用程序一起使用了。