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
  • 相关阅读:
    记录idea run dashboard设置 (微服务项目多服务启动)
    记录Java8中的字符串转数组再通过指定符号拼接
    Java 调用底层接口的几种方法
    工作两个月以后的感想
    几种开源工作流引擎的简单比较
    labin编译的另一种方式
    最近参加一个团队创业项目的感触
    gof设计模式——生成器c++实现
    gof设计模式——抽象工厂 c++实现
    几种开源网络爬虫的简单比较
  • 原文地址:https://www.cnblogs.com/ly77461/p/5708280.html
Copyright © 2011-2022 走看看