0

基本上我的想法是开发一个将在 Windows 中运行的代理

我创建了成功运行的 Windows 服务应用程序,并且我在 Windows 服务中运行的 Windows 服务应用程序中集成了一个 Web 服务代码。

当客户端点击我的网址时如何调用该 Web 服务方法?

如何形成可以调用Web服务方法的url来获取方法返回值?

4

1 回答 1

2

好的,我会试着回答。

  • 假设您要调用REST web service. 你需要什么?AHttpClient和(可能)JSON/XML Serializer。您可以使用内置.NET类或类似的库RestSharp

使用调用 REST Web 服务的示例RestSharp

var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", 123); // replaces matching token in request.Resource

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile(path);

// execute the request
RestResponse response = client.Execute(request);
var content = response.Content; // raw content as string

// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;

// easy async support
client.ExecuteAsync(request, response => {
    Console.WriteLine(response.Content);
});

// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
    Console.WriteLine(response.Data.Name);
});

// abort the request on demand
asyncHandle.Abort();

不需要使用RestSharp,不。对于简单的情况HttpWebRequest(+ DataContractJsonSerializaer或 Xml 模拟)将是完美的

于 2013-04-02T07:45:24.583 回答