zoukankan      html  css  js  c++  java
  • 以Post方式发送数据采用WebClient

    通过Post方式发送数据可以避免Get方式的数据长度限制
    下面采用WebClient来实现这个功能
    Web服务端可以是任何CGI但是要搞清楚Web端接受的编码,代码如下
                WebClient wc = new WebClient();
                StringBuilder postData 
    = new StringBuilder();
                postData.Append(
    "formField1=" + "表单数据一");
                postData.Append(
    "&formField2=" + "表单数据二");
                postData.Append(
    "&formField3=" + "表单数据三");
                
    //下面是GB2312编码
                byte[] sendData = Encoding.GetEncoding("GB2312").GetBytes(postData.ToString());
                wc.Headers.Add(
    "Content-Type""application/x-www-form-urlencoded");
                wc.Headers.Add(
    "ContentLength", sendData.Length.ToString());

                
    byte[] recData= wc.UploadData("http://www.domain.cn/services/DataImport1.asp","POST",sendData);

                
    //显示返回值注意编码
                MessageBox.Show(Encoding.GetEncoding("GB2312").GetString(recData));

    注意"表单数据x"中包含如 "&","=","+"时需要使用,
    HttpUtility.UrlEncode( "+++xxx为什么不编码也可以",Encoding.GetEncoding("GB2312")) 进行编码
    HttpUtility.UrlEncode(string) 默认使用UTF-8进行编码,因此使用 UrlEncode编码时并且字段里有中文,并且目标网站使用GB2312时,需要在UrlEncode函数中指明使用Gb2312
    这样上面的拼接代码可以修改为如下:

     postData.Append("formField1=" +  HttpUtility.UrlEncode("表单数据一",Encoding.GetEncoding("GB2312")));
     postData.Append("&formField2=" +  HttpUtility.UrlEncode("表单数据二",Encoding.GetEncoding("GB2312")));
    ................

  • 相关阅读:
    将Microsoft SQL Server 2000数据库转换成MySQL数据库
    centos7 升级php版本
    Jquery Ajax方法传递json到action
    2015/12/7
    sql server 2008 评估期已过期
    C# 邮件发送注意事项
    ReSharper warning: Virtual member call in a constructor
    EF code first 生成edmx文件
    EF 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
    C# 发送邮件
  • 原文地址:https://www.cnblogs.com/wdfrog/p/924833.html
Copyright © 2011-2022 走看看