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
  • 相关阅读:
    ios自动布局
    Android真机调试流程
    Window phone应用中的多触点手势解读以及toolKit.dll和Microsoft.Phone.dll 冲突问题
    软件质量可视化与软件测试
    软件测试中不可忽视的 Warning
    软件测试作业1 -- 关于c++项目中类相互调用的问题与解决
    软件测试 总结
    系统测试,集成测试,单元测试的联系与区别
    白盒测试
    关于UI测试的一些实例操作
  • 原文地址:https://www.cnblogs.com/ly77461/p/5708280.html
Copyright © 2011-2022 走看看