【问题标题】:Combine PHP Webservice and C# Client结合 PHP Webservice 和 C# 客户端
【发布时间】:2015-10-28 15:27:30
【问题描述】:

我有一个使用 PHP 网络服务的 C#/Winforms 应用程序。第一个 PHP web 服务 uploadFile.php 用于在 mysql 数据库上上传 blob 文件(例如通过 unix 应用程序),另一个 web 服务 getFile.php(下面的代码)用于获取文件(如果文件可用于客户。

目前,客户端每 5 秒调用一次 Web 服务。显然 Apache 无法处理所有的连接并且变慢并且经常宕机或超时,今天我在 2 分钟内计算了 2000 个连接,在 25 分钟内计算了 100 000 个连接。 99% 的人不返回任何文件。

因此,我希望 web 服务 uploadFile 在结果可用时通知客户端,而不是每 5(或 10 或 20 或其他)秒调用一次 web 服务。我不熟悉 SOAP/REST 或任何其他类型的 web 服务,并且出于实际原因(应用程序的旧版本仍在运行),我无法用 REST 替换当前的 web 服务。这是当前 web 服务 getOK.php 所做的:

 if(getJobsForUser($login, $serveur)){

    //data is BLOB content
    $query = "SELECT idjob,fileName,data FROM JobSpool WHERE idcompte=$idcompte LIMIT 1" ;  
    $stmt = $pdo->query($query);
    $res = $stmt->fetch(PDO::FETCH_BOTH);  
    $idjob = $res[0] ;
    $nomfic = $res[1] ;
    $data = $res[2] ;
    //send datas to client
    echo $nomfic."\n" ;
    echo $data ; 
 }

 function checkJobsforUser($login, $serveur){
    global $pdo, $idcompte;

    $query = "SELECT count(*),c.idcompte FROM Compte as c,Job as j WHERE c.login='$login' AND c.serveur = '$serveur' AND j.idcompte = c.idcompte" ;

    $stmt = $pdo->query($query);
    $results = $stmt->fetch(PDO::FETCH_BOTH);
    //print_r($results);
    $count = $results[0];
    $idcompte = $results[1];
    //echo "count : $count, idcompte : $idcompte <br/>";
    if($count > 0)
        return true;
    return false;

}

C#代码:

public String getMyFile(){
    String nomFicFinal = String.Empty;
    try
    {
        HttpWebRequest hwr;
        String request = "http://myWebServiceDomain.com/getFile.php?login=myLogin&otherId=myOtherId";
        hwr = (HttpWebRequest)HttpWebRequest.Create(request);

        using (HttpWebResponse hwrep = (HttpWebResponse)hwr.GetResponse())
        {
            //read datas 
            br = new BinaryReader(hwrep.GetResponseStream());
            Byte[] tmp = new Byte[256];
            int b = br.Read();
            while (b != -1)
            {
                if (b == 10) break;
                tmp[indNomFic++] = (Byte)b;
                b = br.Read();
            }

            //get filename
            nomFicFinal = System.Text.Encoding.ASCII.GetString(tmp, 0, indNomFic);

            //if parsed data == "0" nothing returned
            if (nomFicFinal.Length == 1 && (nomFicFinal.CompareTo("0") == 0 || nomFicFinal.CompareTo("1") == 0))
            {
                br.Close();
                nomFicFinal = "";
            }
            else //we parse the content of the request and we create the file with the name nomFicFinal in ApplicationData
            {
                nomFicFinal = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MyApp\" + nomFicFinal;
                bw = new BinaryWriter(File.Open(nomFicFinal, FileMode.Create));

                tmp = new Byte[1024];
                while ((tmp = br.ReadBytes(1024)) != null)
                {
                    if (tmp.Length == 0) break;
                    bw.Write(tmp);
                    bw.Flush();
                }
                br.Close();
                bw.Close();
            }
        }
    }catch(Exception ex){
        //do something
    }

    return nomFicFinal;

}

我一直在搜索“回调 PHP C#”等,但我暂时还不清楚该怎么做。

【问题讨论】:

  • 究竟是什么击中了什么? c# 应用程序正在访问 php 应用程序?您的问题中的“客户”是什么?
  • 一个 Unix 应用程序点击了 uploadFile.php。在此期间,C# 应用程序会定期点击 getFile.php。希望更清楚
  • 那么unix应用上传文件,c#应用获取文件?什么不直接上传文件到c#?
  • 因为我们有许多客户端和不同的 unix 服务器托管 unix 应用程序,有时不在同一个区域。 C# 是一个客户端应用程序(由用户提供)。但是你的意思是直接上传到 C# 是什么意思?
  • 我的意思是,如果 c# 是发出请求的那个,并且他每 5 秒轮询一次服务器,它不应该造成那么大的负载。如果请求在

标签: c# php web-services callback


【解决方案1】:

我一直这样做。不要轮询网络服务!

从 Winforms 调用 PHP 网络服务(对不起,我仍然停留在 VB 中):

Public Function callServer(ByVal strURL As String, ByVal strCallString As String, Optional ByVal intWaitTimeIncrease As Integer = 0) As String

        'USE:  strResult = callServer(My.Settings.sf_GetData, SQL_Composite)

        callServer = ""

            If strCallString <> "" Then

                debugLog(strURL & " -> " & strCallString)

                strCallString = System.Web.HttpUtility.UrlEncode(strCallString) 'IF WE DONT ENCODE THIS HERE THEN ? & AND MAYBE OTHER CHARACTERS GET REMOVED

                Dim wRequest As HttpWebRequest = HttpWebRequest.Create(New Uri(strURL)) ' & "?SQL=" & Uri.EscapeDataString(SQL_Composite)))

                With wRequest

                    ' .AllowWriteStreamBuffering = False

                    .CookieContainer = ClientSettings.wsCookies
                    .Timeout = My.Settings.WebserviceTimeout + 5000 + My.Settings.WebserviceTimeoutUserAdjustment + intWaitTimeIncrease
                    .Proxy = ClientSettings.myWebProxy
                    .KeepAlive = False
                    .ContentType = "application/x-www-form-urlencoded" '"text/plain"
                    .Method = WebRequestMethods.Http.Post

                    Dim bPostData As Byte() = Text.Encoding.UTF8.GetBytes("SQL=" & strCallString)

                    .ContentLength = bPostData.Length

                    Using dataStream = .GetRequestStream()
                        dataStream.Write(bPostData, 0, bPostData.Length)
                    End Using

                    Using httpWebResponse = TryCast(.GetResponse(), HttpWebResponse)

                        If httpWebResponse IsNot Nothing Then

                            Dim responseUri As String = httpWebResponse.ResponseUri.AbsoluteUri

                            .CookieContainer.Add(httpWebResponse.Cookies)

                            Using streamReader = New StreamReader(httpWebResponse.GetResponseStream())
                                callServer = streamReader.ReadToEnd()
                            End Using

                            If callServer.StartsWith(vbCrLf) Then callServer = replaceFirstFound(callServer, vbCrLf, "")
                            If callServer.StartsWith(vbLf) Then callServer = replaceFirstFound(callServer, vbLf, "")

                        End If
                    End Using

                End With
            End If

    End Function

PHP

function exchangeData($compositeSQL) {
    //HERE YOU WRITE YOUR DATABASE CALLS AND RETURN INFORMATION
    //THEN YOU JUST 'return (string) x;' AS A PRODUCT OF THE FUNCTION
}

if (!empty($_POST["SQL"])) {
    echo (string) exchangeData($_POST["SQL"]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    相关资源
    最近更新 更多