本帖最后由 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;
}
} |