[英]C# Client to Consume Google App Engine RESTful Webservice (rpc XML)
我认为使用C#客户端使用Google App Engine Web服务时遇到了问题。 我使用的Google App Engine代码在这里 。 这是服务器上的python脚本的样子:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import logging
from StringIO import StringIO
import traceback
import xmlrpclib
from xmlrpcserver import XmlRpcServer
class Application:
def __init__(self):
pass
def getName(self,meta):
return 'example'
class XMLRpcHandler(webapp.RequestHandler):
rpcserver = None
def __init__(self):
self.rpcserver = XmlRpcServer()
app = Application()
self.rpcserver.register_class('app',app)
def post(self):
request = StringIO(self.request.body)
request.seek(0)
response = StringIO()
try:
self.rpcserver.execute(request, response, None)
except Exception, e:
logging.error('Error executing: '+str(e))
for line in traceback.format_exc().split('\n'):
logging.error(line)
finally:
response.seek(0)
rstr = response.read()
self.response.headers['Content-type'] = 'text/xml'
self.response.headers['Content-length'] = "%d"%len(rstr)
self.response.out.write(rstr)
application = webapp.WSGIApplication(
[('/xmlrpc/', XMLRpcHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
客户端(在Python中)是这样的:
import xmlrpclib
s = xmlrpclib.Server('http://localhost:8080/xmlrpc/')
print s.app.getName()
使用Python客户端从Google App Engine检索值没有问题,但是使用C#客户端检索值确实有困难。 当我尝试从Web请求中获取GetResponse
, 404 method not found
的错误。
这是我的代码
var request = (HttpWebRequest)WebRequest.Create("http://localhost:8080/xmlrpc/app");
request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "text/xml";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) //404 method not found error here.
{
}
编辑:对于终点,我尝试了:
但是没有办法
我认为网址一定是错误的,但我不知道如何正确处理。 任何想法?
我猜发生了什么事,就是您使用HttpWebRequest发送的请求缺少实际内容; 应该是xml格式的rpc方法信息。 请检查以下代码是否适合您; 它应该将请求发送到http:// localhost:8080 / xmlrpc /并将生成的xml转储到控制台中。
// send request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8080/xmlrpc/");
request.Method = "POST";
request.ContentType = "text/xml; encoding=utf-8";
string content = "<?xml version='1.0'?><methodCall><methodName>app.getName</methodName><params></params></methodCall>";
byte[] contentBytes = System.Text.UTF8Encoding.UTF8.GetBytes(content);
request.ContentLength = contentBytes.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(contentBytes, 0, contentBytes.Length);
}
// get response
WebResponse response = request.GetResponse();
XmlDocument xmlDoc = new XmlDocument();
using (Stream responseStream = response.GetResponseStream())
{
xmlDoc.Load(responseStream);
Console.WriteLine(xmlDoc.OuterXml);
}
希望这有帮助,问候
除了此处发布的出色答案外,还可以使用xml-rpc.net来完成这项工作。
这是服务器端的代码,假设getName现在带有一个string
参数:
def getName(self,meta, keyInput):
return keyInput
通过使用xml-rpc.net,这将是C#客户端代码:
[XmlRpcUrl("http://localhost:8080/xmlrpc")]
public interface IMath : IXmlRpcProxy
{
[XmlRpcMethod("app.getName")]
string GetName(string number);
}
public string GetName(string keyInput)
{
var mathProxy = XmlRpcProxyGen.Create<IMath>();
mathProxy.Url = "http://localhost:8080/xmlrpc/";
return mathProxy.GetName(keyInput);
}
希望这对所有努力从C#客户端向GAE进行rpc调用的人有所帮助。
您的App Engine应用中该端点的正则表达式完全是'/ xmlrpc /',这是您在Python测试中使用的正则表达式,但是在C#客户端中,您使用的是'/ xmlrpc / app',但未映射到任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.