gpt4 book ai didi

c# - 以编程方式使用 POST 发送表单

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:56 25 4
gpt4 key购买 nike

我需要通过 POST 以编程方式发送表单。我有 4 个字段,一个复选框和一个提交按钮。我该怎么做?

最佳答案

假设您的问题是关于表单应用程序,我使用这些函数。
你可以打电话

HttpPost(
post_url,
"field_name_1", value_1,
"field_name_2", value_2,
...);

他们在这里:

public static string HttpPost(string url, params object[] postData)
{
StringBuilder post = new StringBuilder();
for (int i = 0; i < postData.Length; i += 2)
post.Append(string.Format("{0}{1}={2}", i == 0 ? "" : "&", postData[i], postData[i + 1]));
return HttpPost(url, post.ToString());
}
public static string HttpPost(string url, string postData)
{
postData = postData.Replace("\r\n", "");
try
{
WebRequest req = WebRequest.Create(url);
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;

Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();

WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
return returnvalue;
}
catch (Exception ex)
{
Debug.WriteLine("POST Error on {0}\n {1}", url, ex.Message);
return "";
}
}

关于c# - 以编程方式使用 POST 发送表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7737786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com