如何调用restful webservice

时间:2012-11-28 12:43:38

标签: c# asp.net web-services

如何使用webservice

连接到任何宁静的C#

我有以下有关webservice

的信息

BASE_URL是托管URL的{​​{1}}。然后,此WebServices后跟所需的BASE_URL名称,然后是所需的GROUP名称。

例如,

METHOD

这将提供完整的URL:

BASE_URL = https://www.abcd.com/ws/

GROUP = transaction

METHOD = createTransaction

每次调用都必须包含以下参数(作为POST变量):

https://www.abcd.com/ws/transaction/createTransaction

请帮我一些链接来实现编码。

2 个答案:

答案 0 :(得分:0)

您可以使用HttpClient课程。

 static async void Main()
    {
        try 
           {
      // Create a New HttpClient object.
      HttpClient client = new HttpClient();

      HttpResponseMessage response = await client.PostAsync("https://www.abcd.com/ws/transaction/createTransaction");
      response.EnsureSuccessStatusCode();
      string responseBody = await response.Content.ReadAsStringAsync();
      // Above three lines can be replaced with new helper method in following line 
      // string body = await client.GetStringAsync(uri);

      Console.WriteLine(responseBody);
    }  
    catch(HttpRequestException e)
    {
      Console.WriteLine("\nException Caught!"); 
      Console.WriteLine("Message :{0} ",e.Message);
    }
  }

这不完整,您必须将参数传递给HttpContent对象格式的PostAsync方法。

答案 1 :(得分:0)

这是在asp.net中调用或使用rest webservice的一种技术c

var client = new RestClient("url");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/x-www-form-urlencoded","type=password&user_id=test@gmail.com",ParameterType.RequestBody);
IRestResponse response = client.Execute(request);