-
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里面的电源选项
在此在说明一下,以上信息仅用作以后查阅使用,均不是原著。
原文地址:https://www.cnblogs.com/Wxinchun/p/10884272.html
时间: 2024-11-14 10:56:45