/// <summary>
/// WebServicePost
/// </summary>
/// <param name="url">地址</param>
/// <param name="parameter">参数</param>
/// <param name="strMethodName">方法名</param>
/// <returns></returns>
public static string HttpPostWebService(string url, Dictionary<string,string> parameter,string strMethodName)
{
try
{
string result = string.Empty;
string param = string.Empty;
byte[] bytes = null;
Stream writer = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
foreach (var item in parameter)
{
param += HttpUtility.UrlEncode(item.Key) + "=" + HttpUtility.UrlEncode(item.Value) + "&";
}
// param = param.Remove(param.LastIndexOf(","), 1);
bytes = Encoding.UTF8.GetBytes(param);
request = (HttpWebRequest)WebRequest.Create(url + "/" + strMethodName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
try
{
writer = request.GetRequestStream(); //获取用于写入请求数据的Stream对象
}
catch (Exception ex)
{
return "";
}
writer.Write(bytes, 0, bytes.Length); //把参数数据写入请求数据流
writer.Close();
try
{
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
response = (HttpWebResponse)request.GetResponse(); //获得响应
}
catch (WebException ex)
{
return "";
}
#region 这种方式读取到的是一个返回的结果字符串
Stream stream = response.GetResponseStream(); //获取响应流
XmlTextReader Reader = new XmlTextReader(stream);
Reader.MoveToContent();
result = Reader.ReadInnerXml();
#endregion
#region 这种方式读取到的是一个Xml格式的字符串
//StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//result = reader.ReadToEnd();
#endregion
response.Dispose();
response.Close();
//reader.Close();
//reader.Dispose();
Reader.Dispose();
Reader.Close();
stream.Dispose();
stream.Close();
return result;
}
catch (Exception ex)
{
return ex.Message;
}
}
地址以.asmx结尾。