从C#Client到基于Java的Google App Engine

时间:2010-06-22 13:28:27

标签: c# google-app-engine xml-rpc

我在从C#客户端调用基于Java的Google App Engine服务器时遇到了很多问题

这是我的客户端代码的样子:

 // C# Client
static void Main(string[] args)
{
  const string URL = "http://localhost:8888/googlewebapptest7/greet";
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
  request.Method = "POST";
  request.ContentType = "text/x-gwt-rpc; charset=utf-8";
  string content = "<?xml version='1.0'?><methodCall><methodName>greetServlet.GetName</methodName><params></params></methodCall>";
  byte[] contentBytes = 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();
  using (Stream responseStream = response.GetResponseStream())
  {
    string res = new StreamReader(responseStream).ReadToEnd();

    Console.WriteLine("response from server:");
    Console.WriteLine(res);
    Console.ReadKey();
  }     

服务器基本上是Google默认的Web项目,有一个额外的方法:

public String GetName() { return "HI!"; }

已添加到GreetingServiceImpl。

每次运行我的客户端时,我都会从服务器获得以下异常: An IncompatibleRemoteServiceException was thrown while processing this call. com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: This application is out of date, please click the refresh button on your browser. ( Malformed or old RPC message received - expecting version 5 )

我想将其保留在普通的HTTP请求中。

知道发生了什么事吗?

2 个答案:

答案 0 :(得分:1)

正如尼克指出你试图模仿GWT的RPC格式。我也试过了:))

然后我采取了不同的方法。我使用Google Protocol Buffers作为HTTP(S)上的编码器/解码器。

我的一个项目是用C#编写的桌面应用程序。服务器端也是C#.Net。当然,我们使用WCF作为传输。

您可以将协议缓冲区插入到WCF传输中。您将为C#客户端和Java服务器提供相同的接口配置。这很方便。

当我不太忙于工作时,我会用代码示例更新这个答案。

答案 1 :(得分:0)

我从未找到过在Google App Engine上使用基于XML的RPC的好方法。相反,我找到了这个优秀的JSON库和教程:

http://werxltd.com/wp/portfolio/json-rpc/simple-java-json-rpc/

效果很好!