public void HttpPostWebService()
{
//接口调用地址
string strUrl="http://192.168.0.80:8080/DLCommonFunIF/IF00001.asmx/GetCustomer";
//接口传值参数
string paramData="Customer_ID=1&DeleFlg=0";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST"; //调用接口方式 Post/Get
request.ContentType = "application/x-www-form-urlencoded";
byte[] payload = System.Text.Encoding.UTF8.GetBytes(paramData);
request.ContentLength = payload.Length;
Stream writer;
try
{
//获取用于写入请求数据的Stream对象
writer = request.GetRequestStream();
}
catch (WebException ex)
{
writer = null;
// 错误LOG
Log.WriteLog(LogType.Exception, "调用获取客户信息接口失败:" + ex.Message);
return statusCode=404;
}
//把参数数据写入请求数据流
writer.Write(payload, 0, payload.Length);
writer.Close();
HttpWebResponse response;
try
{
//获得响应流
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
// 错误LOG
Log.WriteLog(LogType.Exception, "调用获取客户信息接口响应失败:" + ex.Message);
return (int)response.StatusCode;
}
statusCode = (int)response.StatusCode; //获取Http响应状态码 200/404/500
Stream stream = response.GetResponseStream();
string s;
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
s = reader.ReadToEnd();//接口返回结果XML格式的字符串
Log.WriteLog(LogType.Exception, "调用接口返回的结果:" + s);
}
}