Cool_Breeze 发表于 2021-6-17 21:03

C# Post 请求求助

本帖最后由 Cool_Breeze 于 2021-6-17 21:32 编辑

Python 代码 Post 请求代码没有问题:
# -*- 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# 代码失败:
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;
    }
}

yunruifuzhu 发表于 2021-6-17 21:03

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

Cool_Breeze 发表于 2021-6-17 21:33

将第15行改为 pcbQuery.ContentType = "application/x-www-form-urlencoded; charset=gb2312";解决!

火之国 发表于 2021-6-17 21:38

Cool_Breeze 发表于 2021-6-17 21:33
将第15行改为 pcbQuery.ContentType = "application/x-www-form-urlencoded; cha ...

你自己采纳自己了:lol

zyh666 发表于 2021-6-17 22:02

用retrofit库 简单{:17_1073:}

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

自己帮了自己可还行
页: [1]
查看完整版本: C# Post 请求求助