zoukankan      html  css  js  c++  java
  • post get

            /// <summary>
            /// Get方法
            /// </summary>
            /// <param name="serverUrl">url地址</param>
            /// <param name="postData">数据 格式为"aa=xx&bb=xxx"</param>
            /// <returns>请求结果</returns>
            public static string HttpGetConnectToServer(string serverUrl, string postData)
            {
                //创建请求  
                var request = (HttpWebRequest)HttpWebRequest.Create(serverUrl + "?" + postData);
                request.Method = "GET";
                //设置上传服务的数据格式  
                request.ContentType = "application/x-www-form-urlencoded";
                //请求的身份验证信息为默认  
                request.Credentials = CredentialCache.DefaultCredentials;
                //请求超时时间  
                request.Timeout = 10000;
                //读取返回消息  
                string res = "";
                try
                {
                    var response = (HttpWebResponse)request.GetResponse();
                    var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = reader.ReadToEnd();
                    reader.Close();
                }
                catch (Exception ex)
                {
    
                }
                return res;
            }
    
            /// <summary>
            /// post方法
            /// </summary>
            /// <param name="postUrl">url地址</param>
            /// <param name="paramData">提交的ParamData对象</param>
            /// <returns></returns>
            public static string PostWebRequest<T>(string postUrl, T paramData)
            {
                string ret = string.Empty;
                try
                {
                    string str_Tem = JsonSerializer(paramData);//将对象转成json串
                    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str_Tem);
                    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                    webReq.Method = "POST";
                    webReq.ContentType = " application/json; charset=utf-8";
                    webReq.ContentLength = byteArray.Length;
                    Stream newStream = webReq.GetRequestStream();
                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                    newStream.Close();
                    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    ret = sr.ReadToEnd();
                    sr.Close();
                    response.Close();
                    newStream.Close();
                }
                catch (Exception ex)
                {
                   
                }
                return ret;
            }
  • 相关阅读:
    初学者常用的LINUX命令
    logging模块全总结
    logging模块初识
    xpath知多少
    selenium常用的API
    系统测试基础(适合入门)
    JavaScript正则表达式(一)
    webpack2学习(二)
    webpack2学习(一)
    SQL语句学习
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/4482045.html
Copyright © 2011-2022 走看看