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
  • 相关阅读:
    适用于SQL Server生产环境DBA的七大技巧
    Android网络收音机项目(源码实例分享)
    利用antzip包来进行解压与压缩
    开发网站客户端第二弹
    Android 4.1源代码今日将发布
    Android 4.1 Jelly Bean(果冻豆) SDK4.1最新下载
    Google I/O 2012 主题演讲直播(第一天)Android 4.1 Jelly Bean们来了
    android水果连连看开发实例【源码下载有背景音乐、音效】
    android魔法泡泡动画分析(附源码)
    优亿移动开放日第十五期:优亿开发行业数据报告
  • 原文地址:https://www.cnblogs.com/ly77461/p/5708280.html
Copyright © 2011-2022 走看看