简体   繁体   English

Webservice返回广告页面,而不是Monodroid应用程序中的数据

[英]Webservice returns an ad page instead of data in monodroid app

I have been testing an app i am writing in Monodroid(Mono for android). 我一直在测试我用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.) 基本上,我是通过php post(http:// [my-domain] / register_php?/ usrname = blabla&usremail = blabla)将一些简单的数据存储到数据库中(我正在使用免费的虚拟主机服务000webhost.com。)

The php page sends back a response a simple string if it has succeded 如果成功,则php页面将简单字符串返回给响应

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

But in my android app I don't receive this simple string back as the answer. 但是在我的Android应用程序中,我没有收到这个简单的字符串作为答案。 Rather I receive a full html page advertising the services 000webhost.com offers. 而是我收到了一个完整的HTML页面,上面刊登了000webhost.com提供的服务。 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) 但是,如果我在chrome浏览器中输入网址,那么一切正常,并且我会收到适当的状态(无论成功与否)

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 非常感谢您的帮助

I would guess that your free web service is putting some other filter in place before your PHP page. 您的免费Web服务将在PHP页面之前放置一些其他过滤器。

It may be using HTTP headers like accept or user-agent to determine whether or not to use your PHP. 它可能使用HTTP标头(例如accept或user-agent)来确定是否使用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. 尝试使用出色的Fiddler2工具中的“请求生成器”(现在称为Composer)来创建HTTP请求-在几分钟之内,您将发现需要发送哪些标头。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM