开发者

Call php from C# for windows 7 mobile

开发者 https://www.devze.com 2023-02-27 10:29 出处:网络
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

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 ur开发者_如何转开发l returns the correct values when i tested it from the browser.

Thanks for the help - Racheal


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消