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
  • 相关阅读:
    Julia出现错误ERROR: LoadError: syntax: try without catch or finally
    tensorflow白话篇
    论秋招中的排序(排序法汇总-------中篇)
    论秋招中的排序(排序法汇总-------上篇)
    红包的收益(笔试)
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli2/Option
    Spark 贝叶斯分类算法
    centos php Zookeeper kafka扩展安装
    SpringBoot tomcat
    flume 整合kafka
  • 原文地址:https://www.cnblogs.com/ly77461/p/5708280.html
Copyright © 2011-2022 走看看