WebClient以POST方式发送Web请求

2023-05-16

本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求Cookies前后一致。其中application/x-www-form-urlencoded会告诉服务器该参数是以param1=value1&param2=value2&param3=value3方式拼接的。

private bool postDataandDownloadFile(string fileName)
{
    string url = "http://www.huiyaosoft.com/test.aspx";
    StringBuilder postData = new StringBuilder();
    postData.AppendFormat("{0}={1}&", "username", "admin");
    postData.AppendFormat("{0}={1}&", "password", "123456");
    postData.AppendFormat("{0}={1}&", "nickname", UrlEncode("辉耀"));
    try
    {
        if (wc == null)
            wc = new System.Net.WebClient();
        wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
        
        // 继承Cookies
        if (!string.IsNullOrEmpty(cookies))
            wc.Headers.Add("Cookie", cookies);

        // Upload the input string using the HTTP 1.0 POST method.
        byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData.ToString());
        // 此处返回的是一个文件
        byte[] byteResult = wc.UploadData(url, "POST", byteArray);
        // 取得Cookies
        cookies = wc.ResponseHeaders["Set-Cookie"];

        if (type == 1 || type == 3)
            writeFile(byteResult, fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    return false;
}

由于设置了"Content-Type"为"application/x-www-form-urlencoded",所以postData必须先进行urlencode。UrlEncode()的作用是将参数进行编码。

public string UrlEncode(string str)
{
    byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
    return System.Web.HttpUtility.UrlEncode(byStr);
}

//写byte[]到fileName  
private bool writeFile(byte[] pReadByte, string fileName)
{
    FileStream pFileStream = null;
    try
    {
        pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
        pFileStream.Write(pReadByte, 0, pReadByte.Length);
    }
    catch
    {
        return false;
    }
    finally
    {
        if (pFileStream != null)
            pFileStream.Close();
    }
    return true;
}

(完)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WebClient以POST方式发送Web请求 的相关文章

随机推荐

  • 【C语言】__attribute__使用

    一 介绍 GNU C 的一大特色就是 attribute 机制attribute 可以设置函数属性 xff08 Function Attribute xff09 变量属性 xff08 Variable Attribute xff09 和类型
  • Ubuntu20.04下CUDA、cuDNN的详细安装与配置过程(图文)

    Ubuntu20 04下CUDA cuDNN的详细安装与配置过程 xff0c 亲测试可用 xff08 图文 xff09 一 NVIDIA xff08 英伟达 xff09 显卡驱动安装1 1 关闭系统自带驱动nouveau2 2 NVIDIA
  • 使用动量(Momentum)的SGD、使用Nesterov动量的SGD

    使用动量 Momentum 的SGD 使用Nesterov动量的SGD 参考 xff1a 使用动量 Momentum 的SGD 使用Nesterov动量的SGD 一 使用动量 Momentum 的随机梯度下降 虽然随机梯度下降是非常受欢迎的
  • Data Uncertainty Learning in Face Recognition

    Data Uncertainty Learning in Face Recognition 建模数据的不确定性对含噪音图像非常重要 xff0c 但对于人脸识别的研究却很少 先驱者的工作 35 通过将每个人脸图像嵌入建模为高斯分布来考虑不确定
  • ENAS代码解读

    ENAS代码解读 参考代码 xff1a https github com TDeVries enas pytorch 数据集 xff1a cifar10 main函数 xff1a span class token keyword def s
  • PC-DARTS Partial Channel Connections for Memory-Efficient Differentiable Architecture Search

    PC DARTS Partial Channel Connections for Memory Efficient Differentiable Architecture Search Abstract 可微体系结构搜索 xff08 DAR
  • deepsort代码解析

    DeepSort代码解析 项目地址 xff1a deepsort span class token keyword if span name span class token operator 61 61 span span class t
  • CBAM

    CBAM 我们提出了卷积块注意力模块 xff08 CBAM xff09 xff0c 这是一个简单而有效的前馈卷积神经网络的注意力模块 给定一个中间特征图 xff0c 我们的模块沿着通道和空间两个独立的维度依次推导注意力图 xff0c 然后将
  • onos2.0编译安装(npm install和 build问题解决)

    onos编译安装 Ubuntu16 04 1 前置下载安装 1 1 前置包安装 sudo apt get install git sudo apt get install python Oracle JDK8 sudo apt get in
  • iDLG Improved Deep Leakage from Gradients

    iDLG Improved Deep Leakage from Gradients 人们普遍认为 xff0c 在分布式学习系统中 xff0c 如协作学习和联合学习等 xff0c 共享梯度不会泄露私人训练数据 最近 xff0c Zhu等人 1
  • Improved Techniques for Training GANs

    Improved Techniques for Training GANs 在这项工作中 xff0c 我们介绍了几种旨在鼓励GANs游戏收敛的技术 这些技术的动机是对非收敛问题的启发式理解 它们导致了半监督学习效果的提高和样本生成的改进 我
  • CONTRASTIVE REPRESENTATION DISTILLATION

    CONTRASTIVE REPRESENTATION DISTILLATION 我们常常希望将表征性知识从一个神经网络转移到另一个神经网络 这方面的例子包括将一个大型网络提炼成一个较小的网络 xff0c 将知识从一种感觉模式转移到另一种感觉
  • torch.distributed.all_gather

    torch distributed all gather
  • Mosaicking to Distill Knowledge Distillation from Out-of-Domain Data

    Mosaicking to Distill Knowledge Distillation from Out of Domain Data 在本文中 xff0c 我们试图解决一项雄心勃勃的任务 xff0c 即域外知识蒸馏 xff08 OOD
  • python 23种常用模式设计总结

    python 23种常用模式设计总结
  • NormFace精简版

    NormFace
  • Snapdragon Neural Processing Engine SDK(教程)

    SNPE提供以下高级API DL Container Loader SNPE使用后缀为dlc的模型文件 xff0c 提供了模型load函数 xff1b Molde Vallidation 检查输入模型与所选择的运行后端是否合法 xff1b
  • YY直播黑盒测试工程师笔试题

    1 请写出二进制数10111101对应的八进制 十六进制 十进制 2 黑盒测试用例的设计方法有哪些 xff1f 3 你所在项目用到的互联网协议有哪些 xff1f 请列举 4 数据库DB有两张表 xff0c 一张学生信息表Student xf
  • python语言和c语言-python和C语言互相调用的几种方式

    Python这些年风头一直很盛 xff0c 占据了很多领域的位置 xff0c Web 大数据 人工智能 运维均有它的身影 xff0c 甚至图形界面做的也很顺 xff0c 乃至full stack这个词语刚出来的时候 xff0c 似乎就是为了
  • WebClient以POST方式发送Web请求

    本例使用WebClient以POST方式发送Web请求并下载一个文件 xff0c 难点是postData的构造 xff0c 发送Web请求时有的网站要求可能要求Cookies前后一致 其中application x www form url