[h5棋牌项目]-12-C++调用C#

参考资料 

跨越语言的障碍:C++/CLI 调用 C# https://www.cnblogs.com/xfuture/p/3728417.html

C++调用C#的DLL实现方法 https://blog.csdn.net/qq_35040828/article/details/78343161

C++通过DLL调用C#代码 https://blog.csdn.net/MoreWindows/article/details/8678431

C++调用C#的dll时,怎么传string类型参数 https://zhidao.baidu.com/question/1368305884347730019.html

C#中HttpWebRequest的用法详解 https://www.cnblogs.com/love201314/p/5029312.html

c#文件操作Streamwriter, streamreader的使用 https://blog.csdn.net/u011555996/article/details/53200203

GZipStream Class https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.gzipstream?view=netframework-4.8

c# jobject 的数据结构的解析 http://www.runoob.com/csharp/csharp-struct.html

Newtonsoft.Json.dll 的使用 (你需要在项目==》引用==》右键添加引用(R),找到Newtonsoft.Json.dll。) https://blog.csdn.net/qq_26712977/article/details/78391207

C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json.Linq;

namespace ClassLibrary1
{
    public class Class1
    {
        /// <summary>
        /// POST_HTML 取得网页资料(POST方式)
        /// </summary>
        /// <param name="Url">网址</param>
        /// <param name="PostData">传递参数</param>
        /// <param name="Cookies">夹带cookies</param>
        /// <returns></returns>
        public string POST_HTML(string Url, string PostData, string Cookies)
        {
            string strData = string.Empty;
            HttpWebRequest myHttpWebRequest = null;
            HttpWebResponse response = null;
            StreamReader sr = null;
            byte[] bytes = Encoding.ASCII.GetBytes(PostData);
            myHttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
            myHttpWebRequest.KeepAlive = true;
            myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, /";
            myHttpWebRequest.Referer = Url;
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; TheWorld)";
            myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
            myHttpWebRequest.Headers.Add("Accept-Language", "ch");
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.Headers.Add("Cache-Control", "no-cache");
            myHttpWebRequest.Headers.Add("Cookie", Cookies);
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.ContentLength = bytes.Length;
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            myHttpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);

            try
            {
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (response.Headers.Get("Content-Encoding") != null)
                {
                    sr = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), Encoding.UTF8);
                }
                else
                {
                    sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                }

                strData = sr.ReadToEnd();
            }
            catch (WebException ex)
            {
                strData = new StreamReader(ex.Response.GetResponseStream())
                .ReadToEnd();

                //txt_output.Text = ex.ToString() + "\r\n";
            }
            return strData;
        }

        public struct ResultModel
        {
            public string GameDate;
            public string League;
            public string HomeTeam;
            public string VisitTeam;
            public string HomeScore_Half;
            public string VisitScore_Half;
            public string HomeScore_All;
            public string VisitScore_All;
        };  

        //解析资料方法
        /// <summary>
        /// CleanData 整理网页资料,放入LIST内
        /// </summary>
        /// <param name="strData">网页资料</param>
        /// <returns></returns>
        public List<ResultModel> CleanData(string strData)
        {
	        List<ResultModel> List_RM = new List<ResultModel>();
	        JObject JarrReturn = JObject.Parse(strData);
	        foreach (JObject match in JarrReturn["db"])
	        {
		        if (!string.IsNullOrEmpty(match["score_h_ht"].ToString()) 
			        && Convert.ToInt16(match["score_h_ht"].ToString()) >= 0) {
			        ResultModel RM = new ResultModel();
			        RM.GameDate = match["startdatetime"].ToString();
			        RM.League = match["league"].ToString();
			        RM.HomeTeam = match["hostteam"].ToString();
			        RM.VisitTeam = match["visitteam"].ToString();
			        RM.HomeScore_Half = match["score_h_ht"].ToString();
			        RM.VisitScore_Half = match["score_v_ht"].ToString();
			        RM.HomeScore_All = match["score_h"].ToString();
			        RM.VisitScore_All = match["score_v"].ToString();
			        List_RM.Add(RM);
		        }
	        }
            return List_RM;
        }
    }
}

C++代码

#include <string>
#include <list>
#using "Debug/ClassLibrary1.dll"
using namespace ClassLibrary1;
using namespace std;

void main(){
	Class1 ^c = gcnew Class1();
	string url = "https ://www.7366h.com/SportsFt/resultData";
	string postdata = "queryDatetime=2019-08-13&page=1&__RequestVerificationToken=Up5n6T1jQuFXWQFljh85e_4s3nNfeJZJHvNK3vm5cxHWQ_eTrZ5963mpUqDY3xl6v8MQjSBvdU2fBVi7kejtfN2l2RZjWgl0Zr22QIyUpzPwQM1GzAggbyNEutgchXjFzCkPdcrJoVK1_6zaXS9fO1t-NArJAOgAstD0kZtBpQ81";
	string cookies = "ASP.NET_SessionId=lliszh3bypgbygmafpjlrhz5; __RequestVerificationToken=QNFr4sniyRkUacXuUceOAyINcMZl2CCRyetng8vp9Q70davZmbYKp09EzzQArJzHhxbNh89UWLZ-3JUduorBhMl9PbLR7tvpL31JVuI00zNLd-eR9heHfuzAO09zVkUmxzgnsMh7IFvXSO9eInr9DQ2";
	System::String^ p1 = gcnew System::String(url.c_str());
	System::String^ p2 = gcnew System::String(postdata.c_str());
	System::String^ p3 = gcnew System::String(cookies.c_str());
	System::String^ data = c->POST_HTML(p1, p2, p3);
	System::Collections::Generic::List<Class1::ResultModel>^ List_RM = gcnew System::Collections::Generic::List<Class1::ResultModel>();
	List_RM = c->CleanData(data);
	for (int i = 0; i < List_RM->Count; i++)
	{
		System::String^ score = List_RM[i].HomeScore_All;
	}
}

 

posted @ 2019-08-16 16:15  byfei  阅读(156)  评论(0编辑  收藏  举报