簡體   English   中英

如何從ASP.Net訪問PHP Web服務?

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

我正在嘗試在C#ASP.Net Web應用程序中使用Web服務。 該服務是用PHP構建的,位於一些不受我控制的遠程服務器上,因此我無法對其進行修改以將元數據或其他內容添加到其中。

當我在Visual Studio 2008中使用“添加Web引用”選項時,收到以下錯誤:

HTML文檔不包含Web服務發現信息。

在嘗試添加以下Web服務時。

https://subreg.forpsi.com/robot2/subreg_command.php?wsdl

Web服務功能在Visual Studio 2008中公開並顯示。但是我無法添加對它的引用以在ASP.Net應用程序中使用。

t3Service“說明

方法__construct()

create_contact()

get_contact()

get_domain_info()

get_last_error_code()

get_last_error_msg()

get_NSSET()

get_owner_mail()

登錄 ( )

register_domain()

register_domain_with_admin_contacts()

renew_domain()

request_sendmail()

send_auth_info()

transfer_domain()

  1. 我還嘗試了wsdl.exe方法,檢索xml並將其復制到wsdl文件並生成代理類。 但是wsdl輸出包含警告,生成的代理類會跳過公開的函數並生成如下內容:

    // CODEGEN:命名空間'urn:t3'中的操作綁定'create_contact'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:忽略命名空間'urn:t3'中的操作綁定'get_contact'。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'get_domain_info'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'get_last_error_code'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'get_last_error_msg'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:忽略命名空間'urn:t3'中的操作綁定'get_NSSET'。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'get_owner_mail'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:忽略命名空間'urn:t3'中的操作綁定'send_auth_info'。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:忽略命名空間'urn:t3'中的操作綁定'transfer_domain'。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'request_sendmail'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:忽略來自命名空間'urn:t3'的操作綁定'login'。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'register_domain'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:忽略命名空間'urn:t3'中的操作綁定'register_domain_with_admin_contacts'。 use = encoded消息中的每個消息部分都必須指定一個類型。 // CODEGEN:命名空間'urn:t3'中的操作綁定'renew_domain'被忽略。 use = encoded消息中的每個消息部分都必須指定一個類型。

編輯:

我為我的手工編寫的類嘗試了這段代碼。

public String makeWebRequest(String methodName)
        {
              // Create the web request  
              HttpWebRequest request = WebRequest.Create("https://subreg.forpsi.com/robot2/subreg_command.php/") as HttpWebRequest;  
              // Add authentication to request  
              request.Credentials = new NetworkCredential("foo@mydomain.com", "bar");
              request.Method = "POST";
              request.ContentType = "text/xml";
              request.Headers.Add("SOAPAction: https://subreg.forpsi.com/robot2/subreg_command.php/" + methodName);

            // Get response  
          using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
             {  
               // Get the response stream  
               StreamReader reader = new StreamReader(response.GetResponseStream());  
               // Console application output  
               //Console.WriteLine(reader.ReadToEnd());
               return reader.ReadToEnd();
             }  
        }

但是當我試圖獲得響應然后它返回

遠程服務器返回錯誤:(500)內部服務器錯誤。

如上所述 - 您必須為此Web服務手動編寫代理“代理”。

手動進行Web服務調用的一個示例 - 您可能需要調整一些方法。

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;
        }

我建議創建可用於生成對象的xsd(使用xsd.exe),然后您可以序列化/反序列化對實際對象的響應和請求。

編輯:GetSoapString()方法

private string GetSoapString()
        {
            StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
            soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
            soapRequest.Append("{0}");
            soapRequest.Append("</soap:Body></soap:Envelope>");
            return soapRequest.ToString();
        }

對史蒂夫的參考

呼叫一個看起來像:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<get_contact>
<id>123</id>
</get_contact>
</soap:Body>
</soap:Envelope>

響應:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:t3" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:get_contactResponse>
         <get_contactReturn xsi:type="xsd:boolean">false</get_contactReturn>
      </ns1:get_contactResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

“webservice”是一個非常通用的術語。 某些類型的Web服務可能實現WSDL - 但它不是必需的。 IIRC需要一個SOAP接口來提供WSDL,而nuSOAP和PHP SOAP擴展都支持WSDL。 所以看起來遠程端沒有正確實現。

編輯: 以前我有一個500內部服務器錯誤,因為我的肥皂串沒有正確格式化。 我分析了從Web服務返回的xml,並通過查看xml構建了我的soap字符串消息。 最后,我在Dan ^的幫助下解決了這個問題,並通過互聯網進行了一些研究。 謝謝丹。

我克服了500服務器錯誤。 現在我能夠得到至少登錄失敗的響應......

public String MyWebServiceCall()
        {
            string strSoapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://www.artwork-systems.com/webway/sessions\" xmlns:types=\"http://www.artwork-systems.com/webway/sessions/encodedTypes\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
            + " <soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
            + " <tns:Login>"
            + " <login xsi:type=\"xsd:string\">Support@foo.net</login>"
            + " <auth xsi:type=\"xsd:string\">bar</auth>"
            + " </tns:Login>"
            + " </soap:Body>"
            + "</soap:Envelope>";

            HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(@"https://subreg.forpsi.com/robot2/subreg_command.php/"));

            req.ContentType = "text/xml; charset=UTF-8";
            req.Method = "POST";
            req.Accept = "text/xml";
            req.Headers.Add("SOAPAction", @"https://subreg.forpsi.com/robot2/subreg_command.php/");
            req.ProtocolVersion = HttpVersion.Version11;
            req.Credentials = CredentialCache.DefaultCredentials;
            //req.Credentials = new NetworkCredential("Support@foo.net", "bar");

            StreamWriter stm = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            stm.Write(strSoapMessage);
            stm.Flush();
            stm.Close();

            HttpWebResponse wr = (HttpWebResponse)req.GetResponse();
            StreamReader srd = new StreamReader(wr.GetResponseStream());
            string resulXmlFromWebService = srd.ReadToEnd();
            return resulXmlFromWebService;
        }

現在下一個問題是傳遞正確的憑據並處理響應以做其他事情......

順便說一句,這是回應....

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:t3" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:loginResponse><loginReturn xsi:type="xsd:boolean">false</loginReturn></ns1:loginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

編輯: 值false是正確的,因為我發送錯誤的憑據。 我以類似的方式調用Web服務的其他功能,並能夠調用Web服務的所有功能並檢索相應的返回值,然后執行其他處理。

感謝所有幫助解決問題的人。

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM