日常经验记录

Posted wxinchun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日常经验记录相关的知识,希望对你有一定的参考价值。

  1. c#post、get请求https接口获取数据

  1      private static CookieContainer m_Cookie = new CookieContainer();
  2         private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
  3 
  4 
  5 /// <summary>  
  6         /// 创建GET方式的HTTP请求  
  7         /// </summary>  
  8         /// <param name="url">请求的URL</param>  
  9         /// <param name="timeout">请求的超时时间</param>  
 10         /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
 11         /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
 12         /// <returns></returns>  
 13         public static string CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
 14         {
 15             ServicePointManager.ServerCertificateValidationCallback += CheckValidationResult;
 16             if (string.IsNullOrEmpty(url))
 17             {
 18                 throw new ArgumentNullException("url");
 19             }
 20             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 21             request.Method = "GET";
 22             request.UserAgent = DefaultUserAgent;
 23             if (!string.IsNullOrEmpty(userAgent))
 24             {
 25                 request.UserAgent = userAgent;
 26             }
 27             if (timeout.HasValue)
 28             {
 29                 request.Timeout = timeout.Value;
 30             }
 31             if (cookies != null)
 32             {
 33                 request.CookieContainer = new CookieContainer();
 34                 request.CookieContainer.Add(cookies);
 35             }
 36             HttpWebResponse rsp = (HttpWebResponse)request.GetResponse();//获取服务器端的响应
 37             string cookieheader = request.CookieContainer.GetCookieHeader(new Uri(url));//获取 HTTP Cookie 标头,该标头包含表示与特定 URI 关联的 System.Net.Cookie 实例的 HTTP Cookie。
 38             m_Cookie.SetCookies(new Uri(url), cookieheader);//设置cookie
 39             Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);//获取服务器端响应的编码
 40             return GetResponseAsString(rsp, encoding);
 41         }
 42         /// <summary>  
 43         /// 创建POST方式的HTTP请求  
 44         /// </summary>  
 45         /// <param name="url">请求的URL</param>  
 46         /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>  
 47         /// <param name="timeout">请求的超时时间</param>  
 48         /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
 49         /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>  
 50         /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
 51         /// <returns></returns>  
 52         public static string CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, CookieCollection cookies)
 53         {
 54             if (string.IsNullOrEmpty(url))
 55             {
 56                 throw new ArgumentNullException("url");
 57             }
 58             HttpWebRequest request = null;
 59             //如果是发送HTTPS请求  
 60             if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
 61             {
 62                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
 63                 request = WebRequest.Create(url) as HttpWebRequest;
 64                 request.ProtocolVersion = HttpVersion.Version10;
 65             }
 66             else
 67             {
 68                 request = WebRequest.Create(url) as HttpWebRequest;
 69             }
 70             request.Method = "POST";
 71             request.ContentType = "application/x-www-form-urlencoded";
 72 
 73             if (!string.IsNullOrEmpty(userAgent))
 74             {
 75                 request.UserAgent = userAgent;
 76             }
 77             else
 78             {
 79                 request.UserAgent = DefaultUserAgent;
 80             }
 81 
 82             if (timeout.HasValue)
 83             {
 84                 request.Timeout = timeout.Value;
 85             }
 86             if (cookies != null)
 87             {
 88                 request.CookieContainer = new CookieContainer();
 89                 request.CookieContainer.Add(cookies);
 90             }
 91             //如果需要POST数据  
 92             if (!(parameters == null || parameters.Count == 0))
 93             {
 94                 StringBuilder buffer = new StringBuilder();
 95                 int i = 0;
 96                 foreach (string key in parameters.Keys)
 97                 {
 98                     if (i > 0)
 99                     {
100                         buffer.AppendFormat("&{0}={1}", key, parameters[key]);
101                     }
102                     else
103                     {
104                         buffer.AppendFormat("{0}={1}", key, parameters[key]);
105                     }
106                     i++;
107                 }
108                 byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
109                 using (Stream stream = request.GetRequestStream())
110                 {
111                     stream.Write(data, 0, data.Length);
112                 }
113             }
114             HttpWebResponse rsp = (HttpWebResponse)request.GetResponse();//获取服务器端的响应
115             string cookieheader = request.CookieContainer.GetCookieHeader(new Uri(url));//获取 HTTP Cookie 标头,该标头包含表示与特定 URI 关联的 System.Net.Cookie 实例的 HTTP Cookie。
116             m_Cookie.SetCookies(new Uri(url), cookieheader);//设置cookie
117             Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);//获取服务器端响应的编码
118             return GetResponseAsString(rsp, encoding);
119         }
120 
121         private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
122         {
123             return true; //总是接受  
124         }

 这个代码不是原著,是从博客园找的,具体链接找不到了,再次记录一下,以备后面使用,

2、c#调用jar包

通过官网下载ikvm-0.42.0.3.zip、ikvmbin-0.42.0.3.zip、openjdk6-b16-stripped.zip这三个文件,解压ikvm-0.42.0.3.zip,进入bin目录,直接运行ikvmc -out:IKVM.dll test.jar  其中IKVM.dll为生成dll的名称test.jar为你要转成dll的jar包,转完之后直接通过程序引用

技术图片

引用完毕后引入名称空间就可以调用里面的类了

技术图片

 

 3、服务器设置bios里面的电源选项

技术图片

 

在此在说明一下,以上信息仅用作以后查阅使用,均不是原著。

 

以上是关于日常经验记录的主要内容,如果未能解决你的问题,请参考以下文章

译文:18个实用的JavaScript代码片段,助你快速处理日常编程任务

Java程序员的日常——经验贴(纯干货)

CSP核心代码片段记录

VsCode 代码片段-提升研发效率

sublimetext3中保存代码片段

微信小程序代码片段