0

我一直在测试我在 Monodroid(Android 的 Mono)中编写的应用程序。基本上,我通过 php 帖子(http://[my-domain]/register_php?/usrname=blabla&usremail=blabla)将一些简单的数据存储到数据库中(我正在使用免费的网络托管服务 000webhost.com。)

如果成功,php 页面将返回一个简单字符串的响应

<?php
         (...)
      if( $success )
      {
          echo "Success";
      }else{
          echo "Failure";
      }
   >

但是在我的 android 应用程序中,我没有收到这个简单的字符串作为答案。相反,我收到一个完整的 html 页面来宣传 000webhost.com 提供的服务。但是,如果我将 url 输入到我的 chrome 浏览器中,一切正常,我会收到适当的状态(无论它是否成功)

这是发布数据的代码:

byte[] post_data = Encoding.ASCII.GetBytes(string.Format("name={0}&email=
{1}",txt_usr_name.Text,txt_usr_email .Text) );
HttpWebRequest wb_request = 
(HttpWebRequest )WebRequest.Create("http://[my-domain]/register_user.php" );
wb_request.Method = "POST";
wb_request.ContentType = "application/x-www-form-urlencoded";
wb_request.ContentLength = post_data.Length;

Stream stream = wb_request.GetRequestStream();
stream.Write( post_data, 0, post_data.Length );
stream.Close();

这是读回响应的代码:

    HttpWebResponse wb_response = ( HttpWebResponse )wb_request.GetResponse();

    string status_code = wb_response.StatusCode.ToString();
    string server = wb_response.Server.ToString();

    Stream answer = wb_response.GetResponseStream();
    StreamReader ans_reader = new StreamReader( answer );

    string result = ans_reader.ReadToEnd();

    AlertDialog.Builder builder = new AlertDialog.Builder( this );
    builder.SetTitle( "Connection result" );
    builder.SetMessage( result );
    builder.SetPositiveButton( "OK", delegate { } );
    var dialog = builder.Create();
    dialog.Show();

当我调用网络服务时,我忘记设置什么了吗?非常感谢您的帮助

4

1 回答 1

0

I would guess that your free web service is putting some other filter in place before your PHP page.

It may be using HTTP headers like accept or user-agent to determine whether or not to use your PHP.

Try using the Request Builder (now called Composer) within the excellent Fiddler2 tool to create HTTP requests - within a few minutes you will find out which headers you need to send.

http://www.fiddler2.com/fiddler2/

于 2013-02-24T10:27:56.503 回答