zoukankan      html  css  js  c++  java
  • Http Post GBK编码参数C#代码示例

         接受请求的网站是GBK编码,Http Post请求参数也需要用GBK编码,以下是一段代码示例

        

                        String sResult = "";
                    Encoding myEncoding 
    = Encoding.GetEncoding("GBK");
                    
    string param = HttpUtility.UrlEncode("merId", myEncoding) + "=" + HttpUtility.UrlEncode(merId, myEncoding)
                        
    + "&" + HttpUtility.UrlEncode("merDate", myEncoding) + "=" + HttpUtility.UrlEncode(merDate, myEncoding)
                        
    + "&" + HttpUtility.UrlEncode("merSeqId", myEncoding) + "=" + HttpUtility.UrlEncode(merSeqId, myEncoding)
                        
    + "&" + HttpUtility.UrlEncode("version", myEncoding) + "=" + HttpUtility.UrlEncode(version, myEncoding)
                        
    + "&" + HttpUtility.UrlEncode("chkValue", myEncoding) + "=" + HttpUtility.UrlEncode(sbValue.ToString(), myEncoding);
                    
    byte[] postBytes = Encoding.ASCII.GetBytes(param);
                    
    try
                    {
                        
                        HttpWebRequest req 
    = (HttpWebRequest)HttpWebRequest.Create("请求地址");
                        req.Method 
    = "POST";
                        req.ContentType 
    = "application/x-www-form-urlencoded;charset=GBK";
                        req.ContentLength 
    = postBytes.Length;
                        Stream webStream 
    = req.GetRequestStream(); //发送数据 
                        webStream.Write(postBytes, 0, postBytes.Length);
                        webStream.Close();
                        
    //获取返回数据
                        HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse();
                        StreamReader reader 
    = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
                        sResult 
    = reader.ReadToEnd();
                        sResult.Trim();
                    }
                    
    catch (Exception e)
                    {
                        sResult 
    = "";
                        
    return sResult;
                    }
                 
  • 相关阅读:
    优化后的组合算法
    Android之——AsyncTask和Handler对照
    LeetCode(30) Substring with Concatenation of All Words
    HDU--1054--Strategic Game【最小点覆盖】
    最小生成树模板(poj3625)
    QT中使用高速排序
    走进APICloud的世界 (1)
    layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案
    解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:
    移动端H5的一些基本知识点总结
  • 原文地址:https://www.cnblogs.com/zycblog/p/1861706.html
Copyright © 2011-2022 走看看