赞
踩
private static string Post(string PostUrl, string Parameters) { string content = string.Empty; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3; //https 请求必需语句,http 请求可省略 //跳过ssl验证 ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; //path不是登录界面,是登录界面向服务器提交数据的界面 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(PostUrl); myReq.Method = "Post"; myReq.ContentType = "application/json"; myReq.Headers.Add("Authorization", "Basic " + GetEncodedCredentials()); //https添加 Basic auth验证 //myReq.Connection = "keep-alive"; myReq.Headers.Add("Cookie", "477cadb5a1f917cda5f5d9cab20f8841=42c7f20d7a509babe62c3e74995d5140; 3c343960f5dd4584c34cd6b38885cefe=000e1bf2bbd8e5ae2dabff0eb27e3b6f"); //myReq.Headers.Add("usercode", "JRFZCX_MYFK"); //myReq.Headers.Add("password", "*&ZHY&*0379");//&JRFZ&01 //填充POST数据 if (Parameters != null) { //转换为字节数组 byte[] bytesRequestData = Encoding.UTF8.GetBytes(Parameters); myReq.ContentLength = bytesRequestData.Length; Stream requestStream = myReq.GetRequestStream(); requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); requestStream.Close(); } else { myReq.ContentLength = 0; } //发送POST数据请求服务器 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); //获取服务器返回信息 Stream myStream = HttpWResp.GetResponseStream(); StreamReader reader = new StreamReader(myStream, Encoding.UTF8); content = reader.ReadToEnd(); reader.Close(); HttpWResp.Close(); } catch (Exception ex) { content = ex.ToString(); } return content; } private string GetEncodedCredentials() { string mergedCredentials = string.Format("{0}:{1}", _username, _password); byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials); return Convert.ToBase64String(byteCredentials); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。