GET和POST传值失败,多半是传输的字符串和URL的事
public static string ExcuteGetToken(string serviceUrl, string ReqInfo, string token)
{
using (null)
{
ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceUrl);
request.CookieContainer = new CookieContainer();
request.Headers.Add("userName", "abc"); //设置信息头用户名
request.Headers.Add("password", ""); //设置信息头密码
request.Headers.Add("Token", token);
request.KeepAlive = true;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//添加发送数据
Encoding encoding = Encoding.GetEncoding("utf-8");
if (!ReqInfo.Equals(""))
{
ReqInfo = "request=" + ReqInfo;
byte[] postData = encoding.GetBytes(ReqInfo);
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, , postData.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//获取返回数据
Stream responseStream = response.GetResponseStream();
if (responseStream == null) return "";
StreamReader streamReader = new StreamReader(responseStream, encoding);
string retString = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return retString;
}
}
post方法
public static string ExcutePostToken(string serviceUrl, string ReqInfo,string token)
{
using (null)
{
ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceUrl);
request.CookieContainer = new CookieContainer();
request.Headers.Add("userName", "abc"); //设置信息头用户名
request.Headers.Add("password", ""); //设置信息头密码
request.Headers.Add("token", token);
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/json";
//添加发送数据
if (!ReqInfo.Equals(""))
{
Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] postData = encoding.GetBytes(ReqInfo);
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, , postData.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//获取返回数据
Stream responseStream = response.GetResponseStream();
if (responseStream == null) return "";
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string retString = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return retString;
}
} private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}