httpwebrequest/httpwebresponse实际项目使用

来源:互联网 发布:敏捷地产 知乎 编辑:程序博客网 时间:2024/05/01 03:22
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Sdw.Common;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string token = "8edf35ed12554d48a42d452ged65a454";     //判断合法,两边一致,则合法
            int kgid = 3;                        //参数:客管Id                     
            int checkstate1 = 8;                //参数:检查状态                     
            string backreason = "no reason";    //参数:异常原因    
            //建立请求     
            HttpWebRequest req = WebRequest.Create("http://xxxxxxxxx/xxxx/xxxxxxxxx") as HttpWebRequest;
            //制定请求方式
            req.Method = "Post";
            //设定请求标头
            req.ContentType = "application/x-www-form-urlencoded";
            //请求流
            Stream stream = req.GetRequestStream();
            //写入流
            using (StreamWriter sw = new StreamWriter(stream))
            {
                //写入参数
                sw.Write("token=" + token + "&csnId=" + kgid + "&checkstate=" + checkstate1 + "&backreason=" + backreason);
            }
            //实例响应对象
            using (WebResponse resp = req.GetResponse())
            {
                result1 result = new result1();
                //实例响应流
                Stream sr = resp.GetResponseStream();
                //读取响应流
                using (StreamReader s = new StreamReader(sr))
                {
                    //返回Json,需以实例转化
                    result = s.ReadToEnd().ToModel<result1>();
                }

                Console.Write(result.msg);
                Console.Read();
            }
        }
    }

    public class result1
    {
        public int Code { set; get; }

        public string msg { set; get; }
    }

}

0 0
原创粉丝点击