-1

How do i connect to any restful webservice using C#

I have the below information about the webservice

The BASE_URL is the URL at which the WebServices are hosted. This BASE_URL is then followed by the required GROUP name, and then the required METHOD name.

For example,

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

GROUP = transaction

METHOD = createTransaction

This would give a complete URL of :

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

Every invocation must contain the following parameters (as POST variables) :

Name

username 

password 

pin 

Please help me with some link to achieve the coding.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
user1448912
  • 35
  • 1
  • 2
  • 7
  • Instead of dublicating your question, try to improve it. http://stackoverflow.com/questions/13591308/calling-webservice-with-base-url – L.B Nov 28 '12 at 12:46
  • Dup of: http://stackoverflow.com/questions/8883656/how-to-consume-a-restful-service-in-net ? – JoelFan Nov 28 '12 at 12:54

2 Answers2

0

You can use HttpClient class.

 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.

Amit Kriplani
  • 682
  • 5
  • 12
0

This is one technique of calling or consuming rest webservice in asp.net 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);
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123