Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to Consume REST Service in C# with multiple parametrs(7 parameters)
Parametrs are-
1.userid
2.pwd
3.requestXML
4.ProductType
5.Product Version
6.reqVolType
7 mbrid,

What I have tried:

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       contentinxml.Load(Server.MapPath("/new.xml"));

       string dataPacketIN = contentinxml.OuterXml;

       URL = "http://test.highmark.in/Inquiry/doGet.service/requestResponse";
       DATA = @"{requestXML=" + dataPacketIN + "userId=" + b2 + ",password=" + b3 + ",productType=" + b5 + ",productVersion=" + b6 + " ,mbrid=" + b4 + ",reqVolType=" + b7 + "}";

       CreateObject();
   }

   private static void CreateObject()
   {
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

       request.Method = "POST";
       request.ContentType = "text/xml";
       request.ContentLength = DATA.Length;
       using (Stream webStream = request.GetRequestStream())
       using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
       {
           requestWriter.Write(DATA);
       }

       //try
       //{
           WebResponse webResponse = request.GetResponse();
           using (Stream webStream = webResponse.GetResponseStream())
           {
               if (webStream != null)
               {
                   using (StreamReader responseReader = new StreamReader(webStream))
                   {
                       string response = responseReader.ReadToEnd();
                       Console.Out.WriteLine(response);


                   }
               }
           }
       

   }
Posted
Updated 17-Oct-16 19:19pm
Comments
Garth J Lancaster 18-Oct-16 1:16am    
Have you thought about using something like RestSharp ? http://restsharp.org/

1 solution

When I see parameters put together like that I think much like I would if I saw SQL concatenation, YUCK !

Here's the example from the RestSharp page

C#
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
IRestResponse 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();


Look at the 'AddParameter' method for example - surely, that's got to be easier ! ... RestSharp - Simple REST and HTTP Client for .NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900