c# เว็บเซอร์ิวิสต้นทาง http ติดต่อกับปลายทาง https

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
jataz2
PHP Super Member
PHP Super Member
โพสต์: 275
ลงทะเบียนเมื่อ: 22/02/2011 11:48 am

c# เว็บเซอร์ิวิสต้นทาง http ติดต่อกับปลายทาง https

โพสต์ที่ยังไม่ได้อ่าน โดย jataz2 »

เว็บเซอร์วิส ที่เครื่องต้นทาง (เครื่อง 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;
		}
	}
}

แก้ไขล่าสุดโดย jataz2 เมื่อ 10/08/2011 12:16 pm, แก้ไขไปแล้ว 1 ครั้ง.
ภาพประจำตัวสมาชิก
imsn
PHP Super Member
PHP Super Member
โพสต์: 375
ลงทะเบียนเมื่อ: 07/05/2010 12:58 pm
ติดต่อ:

Re: c# เว็บเซอร์ิวิสต้นทาง http ติดต่อกับปลายทาง https

โพสต์ที่ยังไม่ได้อ่าน โดย imsn »

เขียน c# ต้องใช้เครื่องมืออะไรดีสุดครับ
คุณ jataz2 ใช้อะไรเขียน
ช่วยตอบเพราะอยากโปรโหมทเว็บ คิคิคลิกเลย
หาเพื่อนฝรั่งคุย M ...
ภาพประจำตัวสมาชิก
jataz2
PHP Super Member
PHP Super Member
โพสต์: 275
ลงทะเบียนเมื่อ: 22/02/2011 11:48 am

Re: c# เว็บเซอร์ิวิสต้นทาง http ติดต่อกับปลายทาง https

โพสต์ที่ยังไม่ได้อ่าน โดย jataz2 »

ลง Visual Studio 2005 หรือ 2008 หรือเวอร์ชั่นใหม่กว่าก็ได้ครับ เเล้วใช้ editor ของตัวมันเองครับ ผมว่า c# เขียนง่ายสุดๆเเล้วครับ เขียน php ยากกว่าอีกครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 20