简体   繁体   English

如何调用restful webservice

[英]How to call restful webservice

How do i connect to any restful webservice using C#我如何连接到任何基于REST的webservice使用C#

I have the below information about the webservice我对以下信息webservice

The BASE_URL is the URL at which the WebServices are hosted. BASE_URL是托管WebServicesURL This BASE_URL is then followed by the required GROUP name, and then the required METHOD name.BASE_URL后跟所需的GROUP名称,然后是所需的METHOD名称。

For example,例如,

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

GROUP = transaction

METHOD = createTransaction

This would give a complete URL of :这将给出一个完整的 URL:

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

Every invocation must contain the following parameters (as POST variables) :每个调用都必须包含以下参数(作为 POST 变量):

Name

username 

password 

pin 

Please help me with some link to achieve the coding.请帮我提供一些链接来实现编码。

You can use HttpClient class.您可以使用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);
    }
  }

This is not complete, you have to pass your parameters to PostAsync method in HttpContent object format.这还没有完成,您必须以 HttpContent 对象格式将参数传递给 PostAsync 方法。

This is one technique of calling or consuming rest webservice in asp.net c#这是在asp.net c#中调用或使用rest webservice的一种技术

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM