调用接口,获取Http响应状态码

11 篇文章 0 订阅
4 篇文章 0 订阅
 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);
            }
           
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值