zoukankan      html  css  js  c++  java
  • C# post请求 HttpWebRequest


    1.参数 paramsValue的格式 要和 Reques.ContentType一致,

    如果 contentype "application/x-www-form-urlencoded" 表单类型,那么 参数为 a=1&b=2 形式

    如果 。。。 "application/json" json 类型 那么参数就为 "{a:1,b:2}" 格式


    接收json

    Stream postData = Request.InputStream;
    StreamReader sRead = new StreamReader(postData);
    string postContent = sRead.ReadToEnd();
    sRead.Close();
    //body是要传递的参数,格式"roleId=1&uid=2"
    //post的cotentType填写:
    //"application/x-www-form-urlencoded"
    //soap填写:"text/xml; charset=utf-8"
    
            public  string PostHttp(string url, string body, string contentType)
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
     
                httpWebRequest.ContentType = contentType;
                httpWebRequest.Method = "POST";
                httpWebRequest.Timeout = 20000;
     
                byte[] btBodys = Encoding.UTF8.GetBytes(body);
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
     
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                string responseContent = streamReader.ReadToEnd();
     
                httpWebResponse.Close();
                streamReader.Close();
                httpWebRequest.Abort();
                httpWebResponse.Close();
     
                return responseContent;
            }
  • 相关阅读:
    视图创建
    根据表格作业题
    表格 作业题练习
    创建表格 练习题
    聚合函数、数学函数、日期时间函数
    接口自动化框架
    python+request+Excel 几十份接口测试用例一起自动化测试的接口测试框架
    python3 函数
    pip源头
    爬虫
  • 原文地址:https://www.cnblogs.com/su-king/p/5121986.html
Copyright © 2011-2022 走看看