zoukankan      html  css  js  c++  java
  • 参数请求post, get , delete中的基本使用(2)

    UTF-8数字编码

    /// <summary>
            /// 参数的Url请求
            /// </summary>
            /// <returns></returns>
            public static string Anlv_RequestPostService(string PostData, string Url)
            {
                try
                {
                    //设置HttpWebRequest基本信息
                    HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Url);
                    myReq.AllowAutoRedirect = false;
                    myReq.Method = "POST";
                    myReq.ContentType = "application/x-www-form-urlencoded";
                    myReq.KeepAlive = true;
    
                    //把数组转换成流中所需字节数组类型
                    Encoding code = Encoding.GetEncoding("UTF-8");
                    byte[] bytesRequestData = code.GetBytes(PostData);
    
                    //填充POST数据
                    myReq.ContentLength = bytesRequestData.Length;
                    Stream requestStream = myReq.GetRequestStream();
                    requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                    requestStream.Close();
    
                    //发送POST数据请求服务器
    
                    HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                    Stream myStream = HttpWResp.GetResponseStream();
    
                    //获取服务器返回信息
    
    
                    StreamReader reader = new StreamReader(myStream, code);
                    StringBuilder responseData = new StringBuilder();
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        responseData.Append(line);
                    }
                    //释放
                    myStream.Close();
    
                    return responseData.ToString();
                }
                catch (Exception ex) { return ""; }
            }
    View Code

    GBK数字编码

    /// <summary>
            /// 参数的Url请求
            /// </summary>
            /// <returns></returns>
            public static string RequestPostService(string PostData,string Url)
            {
                try
                {
                    string XMLPostData="xml=" + PostData+ "";
    
                    //设置HttpWebRequest基本信息
                    HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Url);
                    myReq.AllowAutoRedirect = false;
                    myReq.Method = "POST";
                    myReq.ContentType = "application/x-www-form-urlencoded";
                    myReq.KeepAlive = true;
    
                    //把数组转换成流中所需字节数组类型
                    Encoding code = Encoding.GetEncoding("GBK");
                    byte[] bytesRequestData = code.GetBytes(XMLPostData);
    
                    //填充POST数据
                    myReq.ContentLength = bytesRequestData.Length;
                    Stream requestStream = myReq.GetRequestStream();
                    requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                    requestStream.Close();
    
                    //发送POST数据请求服务器
    
                    HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                    Stream myStream = HttpWResp.GetResponseStream();
    
                    //获取服务器返回信息
    
                    StreamReader reader = new StreamReader(myStream, code);
                    StringBuilder responseData = new StringBuilder();
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        responseData.Append(line);
                    }
                    //释放
                    myStream.Close();
    
                    return responseData.ToString();
                }
                catch (Exception ex) { return ""; }
            }
    View Code
  • 相关阅读:
    [SpringCloud] Hystrix原理及应用
    Excel框设置下拉选项
    The program no longer exists.
    win10 指纹登录修改用户密码
    git 比较两个分支内容差异
    C++ 已知两个时间(年月日)求日期差
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解(SQL数据库和Oracle数据库的区别)
    SQL Server查询优化方法
    xshell连接ubuntu虚拟机的方法
    beyond compare添加Java反编译插件
  • 原文地址:https://www.cnblogs.com/ly77461/p/5708280.html
Copyright © 2011-2022 走看看