吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1320|回复: 8
收起左侧

[已解决] C# Post 请求求助

[复制链接]
Cool_Breeze 发表于 2021-6-17 21:03
30吾爱币
本帖最后由 Cool_Breeze 于 2021-6-17 21:32 编辑

Python 代码 Post 请求代码没有问题:
[Python] 纯文本查看 复制代码
# -*- encoding = utf-8 -*-
import requests
import urllib.parse

header = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
}
form_data = {
    "FileNo" : "44078",
    "Description": "",
    "WriteDate" : "",
    "WriteDate1" : "2021-6-17",
    "WriteBy" : "", 
    "submit" : "++"
}
res = requests.post('xxx.asp', data = form_data, headers=header)
print(res.cookies.values())
 
with open('test.html', 'wb') as f:
    f.write(res.content)


转换为C# 代码失败:
[C#] 纯文本查看 复制代码
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;

class Program
{
    static void Main()
    {

        HttpWebRequest pcbQuery = (HttpWebRequest)WebRequest.Create("xxx.asp");
        pcbQuery.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
        pcbQuery.Method = "POST";
        pcbQuery.ContentType = "text/html; charset=gb2312";
        
        string formData = ProducedFormDate("123");
        pcbQueryWriteFormDate(pcbQuery, formData);
        // return;
        using (StreamWriter fw = new StreamWriter("test.Html", false, Encoding.Default))
        {
            fw.Write(pcbQueryResulted((HttpWebResponse)pcbQuery.GetResponse()));            
        }
    }
    
    // 返回Post表单信息
    static string ProducedFormDate(string pcbNo)
    {
        return string.Format("FileNo={0}&Description=&WriteDate=&WriteDate1={1}&WriteBy=&submit=++",
            pcbNo, DateTime.Today.ToString("yyyy-M-d"));
    }
    
    // 将表单信息写入流
    static void pcbQueryWriteFormDate(HttpWebRequest httpWebRequest, string formData)
    {
        Console.WriteLine(formData);
        Console.WriteLine(formData.Length);
        byte[] formDataBytes =  Encoding.Default.GetBytes(formData);
        using (Stream fw = httpWebRequest.GetRequestStream())
        {
            fw.Write(formDataBytes, 0, formDataBytes.Length);
        }
        Console.WriteLine(httpWebRequest.ContentLength);
    }
    
    // 返回网页内容
    static string pcbQueryResulted(HttpWebResponse Response)
    {
        Stream resStream = Response.GetResponseStream();
        string result = "";
        using (StreamReader fr = new StreamReader(resStream, Encoding.Default))
        {
            result = fr.ReadToEnd();
        }
        return result;
    }
}

最佳答案

查看完整内容

ContentType 决定了你请求参数的类型

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yunruifuzhu 发表于 2021-6-17 21:03
ContentType 决定了你请求参数的类型
 楼主| Cool_Breeze 发表于 2021-6-17 21:33
将第15行改为
[C#] 纯文本查看 复制代码
pcbQuery.ContentType = "application/x-www-form-urlencoded; charset=gb2312";
解决!
火之国 发表于 2021-6-17 21:38
Cool_Breeze 发表于 2021-6-17 21:33
将第15行改为 [mw_shl_code=csharp,true]pcbQuery.ContentType = "application/x-www-form-urlencoded; cha ...

你自己采纳自己了
zyh666 发表于 2021-6-17 22:02
用retrofit库 简单
 楼主| Cool_Breeze 发表于 2021-6-17 22:08
zyh666 发表于 2021-6-17 22:02
用retrofit库 简单

感谢。还没有听说过!哈哈!
 楼主| Cool_Breeze 发表于 2021-6-17 22:11
火之国 发表于 2021-6-17 21:38
你自己采纳自己了

幸好解决的快。不然就浪费大佬们时间了!
Light紫星 发表于 2021-6-18 10:42
推荐easyhttp库
UNITST 发表于 2021-6-20 19:23
自己帮了自己可还行
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 15:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表