โดย jataz2 » 01/03/2011 10:00 am
เว็บเซอร์วิส ที่เครื่องต้นทาง (เครื่อง A เป็น http) ต้องการ invoke ไปยังเครื่องปลายทาง(เครื่อง B)ที่เป็น https เช่น
https://10.0.0.1/service/sub/?id=&code= ............ ถ้าเครื่อง B สร้าง certificate ไว้ใช้เอง ที่เรียกว่า selfsign พวกมาตรฐานจึงมองว่าเป็น Invalid เราต้อง validate ส่วนนี้โดยให้ผลลัพท์การ validate เป็น true ตลอดโดยใช้
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidationCb);
เพื่อทำให้ปลายทาง validate เครื่องเราให้ผ่านได้ทุกครั้ง เเล้วจึงเริ่มส่วนการ invoke ไปที่เครื่องปลาทางที่เป็น https ได้ตามปกติครับ
โค้ด: เลือกทั้งหมด
using System;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Security.Policy;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.IO;
using System.Text;
using log4net;
using log4net.Config;
namespace Util.BLG
{
public class Invoke_SUB
{
private static string invokeUrl = ConfigurationManager.AppSettings["SUB_URL"].ToString();
private string _id;
private string _code;
private string _service;
private string _action;
private string _plan = "";
public static bool MyCertValidationCb(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
public Invoke(string id, string code, string service,string action, string plan)
{
this._id = id;
this._code = code;
this._service = service;
this._action = action;
this._plan = plan;
}
public Invoke(string id, string code, string service, string action)
{
this._id = id;
this._code = code;
this._service = service;
this._action = action;
}
public DataSet SendRequest()
{
DataSet ds = new DataSet();
string xmlString = "";
string sendingQueryData = string.Empty;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidationCb);
WebClient wc = new System.Net.WebClient();
string authInfo = ConfigurationManager.AppSettings["SUB_USER"] + ":" + ConfigurationManager.AppSettings["SUB_PWD"];
authInfo = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(authInfo));
wc.Headers.Add("Authorization", authInfo);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.Headers.Add("Connection", "Alive");
wc.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SUB_USER"].ToString(), ConfigurationManager.AppSettings["SUB_PWD"].ToString());
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id",this._id);
nvc.Add("code",this._code);
nvc.Add("service", this._service);
nvc.Add("action", this._action);
if (this._plan != "")
{
nvc.Add("plan",this._plan);
}
#region GET nvc
for (int i = 0; i < nvc.Count; i++)
{
if (i == 0)
{
sendingQueryData += "?" + nvc.Keys[i] + "=" + nvc[i];
}
else
{
sendingQueryData += "&" + nvc.Keys[i] + "=" + nvc[i];
}
}
#endregion
invokeUrl = invokeUrl + sendingQueryData;
byte[] responseArray;
try
{
responseArray = wc.DownloadData(invokeUrl);
xmlString = Encoding.ASCII.GetString(responseArray);
if (xmlString != null && xmlString.Length > 0)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));
ds.ReadXml(reader);
}
}
catch (Exception ex)
{
}
return ds;
}
}
}
เว็บเซอร์วิส ที่เครื่องต้นทาง (เครื่อง A เป็น http) ต้องการ invoke ไปยังเครื่องปลายทาง(เครื่อง B)ที่เป็น https เช่น https://10.0.0.1/service/sub/?id=&code= ............ ถ้าเครื่อง B สร้าง certificate ไว้ใช้เอง ที่เรียกว่า selfsign พวกมาตรฐานจึงมองว่าเป็น Invalid เราต้อง validate ส่วนนี้โดยให้ผลลัพท์การ validate เป็น true ตลอดโดยใช้
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidationCb);
เพื่อทำให้ปลายทาง validate เครื่องเราให้ผ่านได้ทุกครั้ง เเล้วจึงเริ่มส่วนการ invoke ไปที่เครื่องปลาทางที่เป็น https ได้ตามปกติครับ
[code]
using System;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Security.Policy;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.IO;
using System.Text;
using log4net;
using log4net.Config;
namespace Util.BLG
{
public class Invoke_SUB
{
private static string invokeUrl = ConfigurationManager.AppSettings["SUB_URL"].ToString();
private string _id;
private string _code;
private string _service;
private string _action;
private string _plan = "";
public static bool MyCertValidationCb(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
public Invoke(string id, string code, string service,string action, string plan)
{
this._id = id;
this._code = code;
this._service = service;
this._action = action;
this._plan = plan;
}
public Invoke(string id, string code, string service, string action)
{
this._id = id;
this._code = code;
this._service = service;
this._action = action;
}
public DataSet SendRequest()
{
DataSet ds = new DataSet();
string xmlString = "";
string sendingQueryData = string.Empty;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidationCb);
WebClient wc = new System.Net.WebClient();
string authInfo = ConfigurationManager.AppSettings["SUB_USER"] + ":" + ConfigurationManager.AppSettings["SUB_PWD"];
authInfo = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(authInfo));
wc.Headers.Add("Authorization", authInfo);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.Headers.Add("Connection", "Alive");
wc.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SUB_USER"].ToString(), ConfigurationManager.AppSettings["SUB_PWD"].ToString());
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id",this._id);
nvc.Add("code",this._code);
nvc.Add("service", this._service);
nvc.Add("action", this._action);
if (this._plan != "")
{
nvc.Add("plan",this._plan);
}
#region GET nvc
for (int i = 0; i < nvc.Count; i++)
{
if (i == 0)
{
sendingQueryData += "?" + nvc.Keys[i] + "=" + nvc[i];
}
else
{
sendingQueryData += "&" + nvc.Keys[i] + "=" + nvc[i];
}
}
#endregion
invokeUrl = invokeUrl + sendingQueryData;
byte[] responseArray;
try
{
responseArray = wc.DownloadData(invokeUrl);
xmlString = Encoding.ASCII.GetString(responseArray);
if (xmlString != null && xmlString.Length > 0)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));
ds.ReadXml(reader);
}
}
catch (Exception ex)
{
}
return ds;
}
}
}
[/code]