How to access a PHP Web Service from ASP.Net?

后端 未结 3 1530
北荒
北荒 2021-02-04 15:11

I am trying use a web service in a C# ASP.Net Web Application. The service is built in PHP and is located on some remote server not under my control so I cant modify it to add

3条回答
  •  我在风中等你
    2021-02-04 15:22

    As referenced that - you will have to hand code your "proxy" for this web service.

    One example of manually making a web service call - you may have to tweak the method some.

    private string MakeWebServiceCall(string methodName, string requestXmlString)
            {
                WebRequest webRequest = WebRequest.Create("https://subreg.forpsi.com/robot2/subreg_command.php");
    
                HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
                httpRequest.Method = "POST";
                httpRequest.ContentType = "text/xml";
                httpRequest.Headers.Add("SOAPAction: https://subreg.forpsi.com/robot2/subreg_command.php/" + methodName);
                Stream requestStream = httpRequest.GetRequestStream();
    
                //Create Stream and Complete Request
                StreamWriter streamWriter = new StreamWriter(requestStream);
                streamWriter.Write(String.Format(this.GetSoapString(), requestXmlString));
                streamWriter.Close();
    
                //Get the Response
                WebResponse webResponse = httpRequest.GetResponse();
                Stream responseStream = webResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream);
    
                //Read the response into an xml document
                System.Xml.XmlDocument soapResonseXMLDocument = new System.Xml.XmlDocument();
                soapResonseXMLDocument.LoadXml(streamReader.ReadToEnd());
    
                //return only the xml representing the response details (inner request)
                return soapResonseXMLDocument.GetElementsByTagName(methodName + "Result")[0].InnerXml;
            }
    

    I would recommend creating xsd's which can be used to generate objects (using xsd.exe) and then you can serialized/deserialize responses and requests to actually objects.

    EDIT: GetSoapString() method

    private string GetSoapString()
            {
                StringBuilder soapRequest = new StringBuilder("");
                soapRequest.Append("{0}");
                soapRequest.Append("");
                return soapRequest.ToString();
            }
    

    For Steve's Reference

    Call one looks like:

    
    
    
    123
    
    
    
    

    Response:

    
       
          
             false
          
       
    
    

提交回复
热议问题