如何访问从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服务。
https://subreg.forpsi.com/robot2/subreg_command.php ?WSDL
该网站的服务功能外露,但我不能引用添加到它在ASP.Net应用程序使用Visual Studio 2008中显示
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()
块引用>
我也通过检索XML尝试过的方法Wsdl.exe用并将其复制到一个WSDL文件并生成代理类。但是,WSDL输出包含警告和代理类生成的跳暴露的功能,生成这样的:
// CODEGEN:操作从命名空间绑定create_contact''瓮:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作从命名空间'金塔:T3'绑定'get_contact'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间get_domain_info'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间get_last_error_code'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间get_last_error_msg'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间get_NSSET'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间get_owner_mail'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作结合'send_auth_info从命名空间金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作从命名空间'金塔:T3'绑定'transfer_domain'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间request_sendmail'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作从命名空间'金塔:T3'绑定'登录'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间register_domain'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间register_domain_with_admin_contacts'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型。
// CODEGEN:操作绑定从命名空间renew_domain'金塔:T3'被忽略了。在使用=编码消息的每个消息部分必须指定一个类型
编辑:
我试过这段代码为我手工编码的类。
公共字符串makeWebRequest(字符串方法名)
{
//创建Web请求
HttpWebRequest的要求= WebRequest.Create(https://subreg.forpsi.com/robot2/subreg_command.php/)作为HttpWebRequest的;
//加入身份验证请求,
request.Credentials =新的NetworkCredential(foo@mydomain.com,酒吧);
request.Method =POST;
request.ContentType =文/ XML;
request.Headers.Add(SOAPAction报:https://subreg.forpsi.com/robot2/subreg_command.php/+方法名);
//使用(HttpWebResponse响应= request.GetResponse()作为HttpWebResponse)
{
//获取响应流
StreamReader的阅读器上获得响应
=新的StreamReader(response.GetResponseStream());
//控制台应用程序的输出
//Console.WriteLine(reader.ReadToEnd());
返回reader.ReadToEnd();
}
}
但是,当我试图得到响应,那么它将返回
远程服务器返回错误:(500)内部服务器错误
块引用>
解决方案由于引用的 - 你将不得不手工代码的代理这个Web服务
手工制作Web服务调用的一个例子 - 你可能需要调整一些方法
私人字符串MakeWebServiceCall(串方法名,字符串requestXmlString)
{
的WebRequest的WebRequest = WebRequest.Create(https://subreg.forpsi.com/robot2/subreg_command.php);
HttpWebRequest的的HttpRequest =(HttpWebRequest的)的WebRequest;
httpRequest.Method =POST;
httpRequest.ContentType =文/ XML;
httpRequest.Headers.Add(SOAPAction报:https://subreg.forpsi.com/robot2/subreg_command.php/+方法名);
流requestStream = httpRequest.GetRequestStream();
//创建流和完整的申请
的StreamWriter的StreamWriter =新的StreamWriter(requestStream);
streamWriter.Write(的String.Format(this.GetSoapString(),requestXmlString));
streamWriter.Close();
//得到响应
WebResponse类WebResponse类= httpRequest.GetResponse();
流responseStream = webResponse.GetResponseStream();
的StreamReader StreamReader的=新的StreamReader(responseStream);
//读取响应到XML文档
System.Xml.XmlDocument soapResonseXMLDocument =新System.Xml.XmlDocument();
soapResonseXMLDocument.LoadXml(streamReader.ReadToEnd());
//只返回XML表示响应的细节(内部要求)
返回soapResonseXMLDocument.GetElementsByTagName(方法名+结果)[0] .InnerXml;
}
我会建议创建XSD的可用于生成对象(使用XSD。 exe文件),然后你可以序列化/反序列化响应和请求来反对
编辑:
GetSoapString()方法。私人字符串GetSoapString()
{
StringBuilder的soapRequest =新的StringBuilder(<肥皂:信封的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/\><肥皂:身体与GT;);
soapRequest.Append({0});
soapRequest.Append(< / SOAP:身体与GT;< / SOAP:信封>);
返回soapRequest.ToString();
}
有关史蒂夫的参考
呼叫一个看起来像:
<肥皂:信封的xmlns:XSI =http://www.w3.org / 2001 / XML模式实例
的xmlns:XSD =http://www.w3.org/2001/XMLSchema
的xmlns:SOAP =http://schemas.xmlsoap.org/soap /信封/>
<肥皂:身体与GT;
< get_contact>
<&ID GT 123 LT; / ID>
< / get_contact>
< / SOAP:身体与GT;
< / SOAP:信封>
响应:
< SOAP-ENV:信封SOAP-ENV:encodingStyle的=http://schemas.xmlsoap.org/soap/encoding/的xmlns:SOAP-ENV =http://schemas.xmlsoap.org / SOAP /信封/的xmlns:NS1 =瓮:T3的xmlns:XSD =http://www.w3.org/2001/XMLSchema中的xmlns:XSI =http://www.w3.org/2001 / XML模式实例的xmlns:SOAP-ENC =http://schemas.xmlsoap.org/soap/encoding/>
< SOAP-ENV:身体与GT;
< NS1:get_contactResponse>
< get_contactReturn的xsi:type =XSD:布尔>假LT; / get_contactReturn>
< / NS1:get_contactResponse>
< / SOAP-ENV:身体与GT;
< / SOAP-ENV:信封>
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 meta data or something else into it.
When I use the "Add Web Reference" option in Visual Studio 2008, I receive the following error:
The HTML document does not contain Web service discovery information.
while trying to add the following web service.
https://subreg.forpsi.com/robot2/subreg_command.php?wsdl
The web service functions are exposed and displayed in Visual Studio 2008. however i could not add the reference to it for use in ASP.Net Application.
t3Service" Description
Methods __construct ( )
create_contact ( )
get_contact ( )
get_domain_info ( )
get_last_error_code ( )
get_last_error_msg ( )
get_NSSET ( )
get_owner_mail ( )
login ( )
register_domain ( )
register_domain_with_admin_contacts ( )
renew_domain ( )
request_sendmail ( )
send_auth_info ( )
transfer_domain ( )
I also tried the wsdl.exe method by retrieving the xml and copying it to a wsdl file and generating a proxy class. But the wsdl output contains warnings and the proxy class generated skips the exposed functions and generates something like this:
// CODEGEN: The operation binding 'create_contact' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'get_contact' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'get_domain_info' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'get_last_error_code' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'get_last_error_msg' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'get_NSSET' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'get_owner_mail' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'send_auth_info' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'transfer_domain' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'request_sendmail' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'login' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'register_domain' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'register_domain_with_admin_contacts' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type. // CODEGEN: The operation binding 'renew_domain' from namespace 'urn:t3' was ignored. Each message part in an use=encoded message must specify a type.
Edit:
I tried this piece of code for my hand coded class.
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(); } }
But when I try to get response then it returns
The remote server returned an error: (500) Internal Server Error.
解决方案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("<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(); }
For Steve's Reference
Call one looks like:
<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>
Response:
<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>
这篇关于如何访问从ASP.Net一个PHP Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!