如何从ASP.Net访问PHP Web服务?

我正在尝试在C#ASP.Net Web应用程序中使用Web服务。 该服务是用PHP构建的,位于一些不受我控制的远程服务器上,因此我无法对其进行修改以将元数据或其他内容添加到其中。

当我在Visual Studio 2008中使用“添加Web引用”选项时,收到以下错误:

HTML文档不包含Web服务发现信息。

在尝试添加以下Web服务时。

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

Web服务function在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(""); soapRequest.Append("{0}"); soapRequest.Append(""); return soapRequest.ToString(); } 

史蒂夫参考

呼叫一个看起来像:

    123    

响应:

    false    

“webservice”是一个非常通用的术语。 某些类型的Web服务可能实现WSDL – 但它不是必需的。 IIRC需要一个SOAP接口来提供WSDL,而nuSOAP和PHP SOAP扩展都支持WSDL。 所以看起来远程端没有正确实现。

编辑: 以前我有500内部服务器错误,因为我的肥皂串格式不正确。 我分析了从Web服务返回的xml,并通过查看xml构建了我的soap字符串消息。 最后,我在Dan ^的帮助下解决了这个问题,并通过互联网进行了一些研究。 谢谢丹。

我克服了500服务器错误。 现在我能够得到至少登录失败的响应…

 public String MyWebServiceCall() { string strSoapMessage = "" + "" + " " + " " + " Support@foo.net" + " bar" + " " + " " + ""; 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; } 

现在下一个问题是传递正确的凭据并处理响应以执行其他操作…

顺便说一句,这是回应….

  false 

编辑: 值false是正确的,因为我发送错误的凭据。 我以类似的方式调用Web服务的其他function,并能够调用Web服务的所有function并检索相应的返回值,然后执行其他处理。

感谢所有帮助解决问题的人。

问候