martes, 1 de octubre de 2013

Hacer una llamada por REST del tipo GET desde .NET (Cogido de otro post)

void CallGetMethod()
{
 // Restful service URL            
 string url = "http://localhost/wcdir/service.svc/GetSampleMethod/str/method1";
 string strResult = string.Empty;
 // declare httpwebrequet wrt url defined above            
 HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

 // set method as post            
 webrequest.Method = "GET";

 // set content type            
 webrequest.ContentType = "application/json";

 // declare & read response from service            
 HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

 // set utf8 encoding            
 Encoding enc = System.Text.Encoding.GetEncoding("utf-8");

 // read response stream from response object            
 StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);

 // read string from stream data           
 strResult = loResponseStream.ReadToEnd();

 // close the stream object            
 loResponseStream.Close();

 // close the response object            
 webresponse.Close();

 // assign the final result to text box
 txtResult.Text = strResult;
}

No hay comentarios:

Publicar un comentario