douzou8074 2017-12-07 10:56
浏览 71
已采纳

使用C#和PHP从远程服务器获取回复

I'm trying to get data from a remote server using C#. I tried using a simple script to see if "it works", but I'm not getting a response. The same script works on a local server, so I'd like to know how I can do it.

This is my C# code:

IEnumerator Sayhi()
{
    WWWForm sendInfo = new WWWForm();
    sendInfo.AddField("function", function);

    WWW www = new WWW(bl_LoginDataBase.Instance.GetUrl(bl_LoginDataBase.URLType.CallFunction), sendInfo);
    yield return www;
    Debug.Log(www.text);
}

And the PHP code:

<?php
echo "Hi";

I expected the Debug.Log(www.text); to print Hi, which it does if I use a local machine (http://192.168.0.whatever), but the remote server (http://whatever.example.com) doesn't return anything. I tried making the php fail so it returns an error, make a database and return some values from there, but nothing. I'd like to point out it does work on a local server, and works as intended.

What am I doing wrong?

Edit: By the way, if I access http://www.whatever.example.com/Function.php via browser, it shows the echo result.

  • 写回答

1条回答 默认 最新

  • doulian7305 2017-12-07 11:12
    关注

    C# has built in classes and methods to help you perform such tasks.You can use the WebClient class to connect to web servers (using GET or POST) and even send form values to it easily. See below:

    string url ="http://www.whatever.example.com/Function.php";
    
    var reqparam = new System.Collections.Specialized.NameValueCollection();
    
    reqparam.Add("name", "John Doe");
    
    try
    {
        using (System.Net.WebClient client = new System.Net.WebClient())
        {
    
         byte[] responsebytes = client.UploadValues(url, "POST", parameters);
    
         string output = System.Text.Encoding.UTF8.GetString(responsebytes);
    
         }
    }
    catch (Exception x)
    {
       //an error occured
    }
    

    The variable output will contain the response you want "Hi".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能