lunes, 14 de octubre de 2013

AcceptAllCertifications method to access https

public static bool AcceptAllCertifications (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
 return true;
}

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

Hacer una llamada por REST del tipo POST desde .NET

 public static void CallPostMethod()
        {
 
            //Para el https
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
 
            // Restful service URL
            string url = https://ipv4.fiddler/servicio.svc/metodo;
 
            // declare ascii encoding
            ASCIIEncoding encoding = new ASCIIEncoding();
            string strResult = string.Empty;
            // sample xml sent to Service & this data is sent in POST
            string SampleXml = 
                                @"{ ""strOf"":""strOf"",
                                        ""strTran"":""strTran"",
                                        ""strTipoC"":""strTipoC"",
                                        ""strNumC"":""strNumC"",
                                        ""dtmFecha"" : ""\/Date(1255131630400)\/"" }";
 
            string postData = SampleXml.ToString();
            // convert xmlstring to byte using ascii encoding
            byte[] data = encoding.GetBytes(postData);
            // declare httpwebrequet wrt url defined above
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
            // set method as post
            webrequest.Method = "POST";
            // set content type
            webrequest.ContentType = "application/json";
            // set content length
            webrequest.ContentLength = data.Length;
            // get stream data out of webrequest object
            Stream newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            // 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();
 
 
        }

Examinar direcciones en localhost con fiddler

https://ipv4.fiddler/
 en lugar de https://localhost