从 C# 为 Windows 7 Mobile 调用 php

发布于 2024-11-02 17:58:52 字数 645 浏览 0 评论 0原文

我是 C#、php 和移动 win 7 上的整个编码的初学者。我的代码有问题,并且没有更多的论坛/网站可以搜索答案,所以我在这里发布查询。

我正在开发一个 Windows 7 移动项目,在我的代码中,我需要从 C# 调用 php 脚本,然后使用 php 返回的值。代码类似于:

string url = "../test.php";
req = (HttpWebRequest)WebRequest.Create(url);
IAsyncResult res = (IASync)req.BeginGetResponse(WebComplete, req);

private void Webcomplete(IAsyncResult a)
{
  var req = (HttpWebRequest)a.AsyncState;
  var res = req.EndGetResponse(a);
  ........
  ........
}

代码运行但没有任何反应。当我在 BeginGetResponse 处放置断点时,我发现它根本不调用 WebComplete。您知道为什么会发生这种情况以及解决方法是什么吗?另外,我如何将从 php 代码中获得的值分配给变量,以便我可以进一步操作数据。 注意:当我从浏览器测试时,url 返回正确的值。

感谢您的帮助 - 雷切尔

I have am a beginner in C#, php and the whole coding on mobile win 7. I have a problem with my code and there isnt any more forums/websites that i could search for the answer so i am posting the query here.

I am working on a windows 7 mobile project and in my code i need to call a php script from C# and later use the values that the php returns. The code is something like:

string url = "../test.php";
req = (HttpWebRequest)WebRequest.Create(url);
IAsyncResult res = (IASync)req.BeginGetResponse(WebComplete, req);

private void Webcomplete(IAsyncResult a)
{
  var req = (HttpWebRequest)a.AsyncState;
  var res = req.EndGetResponse(a);
  ........
  ........
}

The code runs but nothing happens. When i put a breakpoint at BeginGetResponse i find it does not call WebComplete at all. Would you know why this is happening and what is the workaround? Also how can i assign the value i get from the php code to a variable, so that i could further manipulate the data.
Note: The url returns the correct values when i tested it from the browser.

Thanks for the help - Racheal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2024-11-09 17:58:52

有两件事要做。

首先,修复 php 文件的 url。正如 @ctacke 提到的,你指向本地设备...它不托管 php 文件,除非你发生了一些非常奇怪的事情。

第二个是可选的。不要执行异步请求,而是使其同步。例如:

String postLocation = "http://myserver.com/test.php";

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postLocation);
// Setting the useragent seems to resolve certain issues that *may* crop up with certain servers.  
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) {
    StreamReader reader = new StreamReader(httpResponse.GetResponseStream());
    string results = reader.ReadToEnd();
    // at this point you have the entire response in the results variable.  
    // Do with it as you please...
}

至少,如果存在真正的问题,同步方法应该立即为您提供有关该问题的反馈。

Two things to do.

First, fix the url to the php file. As @ctacke mentioned the one you have points to the local device... which isn't hosting the php file unless you have some really weird stuff going on.

The second one is optional. Instead of doing an asynchronous request, make it synchronous. For example:

String postLocation = "http://myserver.com/test.php";

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postLocation);
// Setting the useragent seems to resolve certain issues that *may* crop up with certain servers.  
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) {
    StreamReader reader = new StreamReader(httpResponse.GetResponseStream());
    string results = reader.ReadToEnd();
    // at this point you have the entire response in the results variable.  
    // Do with it as you please...
}

At the very least, if there is a real problem the synchronous method should give you immediate feedback on the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文