doujiao1981 2013-02-23 23:39
浏览 72

too long

I have been testing an app i am writing in Monodroid(Mono for android). Basically i am storing some simple data to the database via a php post (http:// [my-domain]/register_php?/usrname=blabla&usremail=blabla) (I am using thre free web hosting service 000webhost.com.)

The php page sends back a response a simple string if it has succeded

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

But in my android app I don't receive this simple string back as the answer. Rather I receive a full html page advertising the services 000webhost.com offers. But if i enter the url into my chrome browser every thing works fine and I receive the appropriate status (weither it has succeeded or not)

Here is the code to post data:

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();

And here is the code to read back the response:

    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();

Is there something I am forgetting to set when i call the webservice? Thank you very much for your help

  • 写回答

1条回答 默认 最新

  • dongwen5019 2013-02-24 10:27
    关注

    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/

    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名