C# 如何使用C提交http表单#
我有一个简单的html文件,比如C# 如何使用C提交http表单#,c#,.net,html,forms,http-post,C#,.net,Html,Forms,Http Post,我有一个简单的html文件,比如 <form action="http://www.someurl.com/page.php" method="POST"> <input type="text" name="test"><br/> <input type="submit" name="submit"> </form> 编辑:我可能对这个问题还不够清楚 我想写一个C#代码,它以与我将上述html粘贴到文件中、用IE打开
<form action="http://www.someurl.com/page.php" method="POST">
<input type="text" name="test"><br/>
<input type="submit" name="submit">
</form>
编辑:我可能对这个问题还不够清楚
我想写一个C#代码,它以与我将上述html粘贴到文件中、用IE打开并用浏览器提交时完全相同的方式提交此表单。您可以使用该类来完成此操作
例如:
使用系统;
Net系统;
使用系统文本;
使用System.IO;
公开课考试
{
//指定接收请求的URL。
公共静态void Main(字符串[]args)
{
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(args[0]);
//为此请求使用的资源设置一些合理的限制
request.MaximumAutomaticRedirections=4;
request.MaximumResponseHeadersLength=4;
//设置用于此请求的凭据。
request.Credentials=CredentialCache.DefaultCredentials;
HttpWebResponse=(HttpWebResponse)request.GetResponse();
Console.WriteLine(“内容长度为{0}”,response.ContentLength);
Console.WriteLine(“内容类型为{0}”,response.ContentType);
//获取与响应关联的流。
Stream receiveStream=response.GetResponseStream();
//使用所需的编码格式将流传送到更高级别的流读取器。
StreamReader readStream=新的StreamReader(receiveStream,Encoding.UTF8);
Console.WriteLine(“接收到响应流”);
Console.WriteLine(readStream.ReadToEnd());
response.Close();
Close();
}
}
/*
此示例的输出将根据传递到Main的值而变化
但将类似于以下内容:
内容长度为1542
内容类型为text/html;字符集=utf-8
接收到响应流。
...
*/
您的HTML文件不会直接与C#交互,但您可以编写一些C#,使其表现得像HTML文件一样
例如:有一个名为System.Net.WebClient的类,具有简单的方法:
using System.Net;
using System.Collections.Specialized;
...
using(WebClient client = new WebClient()) {
NameValueCollection vals = new NameValueCollection();
vals.Add("test", "test string");
client.UploadValues("http://www.someurl.com/page.php", vals);
}
有关更多文档和功能,请参阅这里是我最近在接收GET响应的网关POST事务中使用的示例脚本。您是否在自定义C#表单中使用此功能?不管您的目的是什么,只要用表单中的参数替换字符串字段(用户名、密码等)
private String readHtmlPage(string url)
{
//setup some variables
String username = "demo";
String password = "password";
String firstname = "John";
String lastname = "Smith";
//setup some variables end
String result = "";
String strPost = "username="+username+"&password="+password+"&firstname="+firstname+"&lastname="+lastname;
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally {
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
Response.Write(“try{this.submit();}catch(e){}”);
我在MVC中遇到了类似的问题(这导致了这个问题)
我从WebClient.UploadValues()请求收到一个字符串形式的表单响应,然后我必须提交该请求-因此我不能使用第二个WebClient或HttpWebRequest。此请求返回了字符串
using (WebClient client = new WebClient())
{
byte[] response = client.UploadValues(urlToCall, "POST", new NameValueCollection()
{
{ "test", "value123" }
});
result = System.Text.Encoding.UTF8.GetString(response);
}
我的解决方案可以用来解决OP问题,它是在代码末尾附加一个Javascript自动提交,然后使用@Html.Raw()在Razor页面上呈现它
result += "<script>self.document.forms[0].submit()</script>";
someModel.rawHTML = result;
return View(someModel);
我希望这可以帮助那些发现自己处于同样情况的人。我需要一个按钮处理程序,它可以在客户端浏览器中创建一个表单发布到另一个应用程序。我回答了这个问题,但没有找到适合我的情况的答案。这就是我想到的:
protected void Button1_Click(object sender, EventArgs e)
{
var formPostText = @"<html><body><div>
<form method=""POST"" action=""OtherLogin.aspx"" name=""frm2Post"">
<input type=""hidden"" name=""field1"" value=""" + TextBox1.Text + @""" />
<input type=""hidden"" name=""field2"" value=""" + TextBox2.Text + @""" />
</form></div><script type=""text/javascript"">document.frm2Post.submit();</script></body></html>
";
Response.Write(formPostText);
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
var formPostText=@”
document.frm2Post.submit();
";
响应。写入(formPostText);
}
此表单应该位于用户页面上还是您希望以编程方式复制的内容?不,这是我希望以编程方式复制的内容。我不理解您的问题。不能直接提交带有服务器端代码的表单。HTML表单提交是在客户端完成的。好的,我在这里查找客户端代码。请您在您的案例中显示解决方案,好吗?不,这只是发送一个URL。我问的是如何以编程方式构造和提交表单。这也会发布帖子(参见WebRequest.Method),正如@JC所说,这只回答了如何发送帖子请求;它不回答如何在HTTP请求中构造表单。如果我使用此代码,我提交到的页面将无法获取信息。它只在使用我引用的html代码段时才起作用。@JC:为什么这个解决方案不起作用?你没有得到你的“测试”输入值?在看到所有这些不被接受的答案后,我自己去解决这个问题,结果发现我做的和shanabus做的完全一样。如果您希望以编程方式发布到表单,HttpWebRequest确实是一个解决方案。请参阅本文中的另一个示例,该示例与我提交的页面基本相同,它根据输入发送响应。只有在我通过浏览器执行此操作时,它才会发送正确的响应。也许这与php实现有关,我不知道。或者,正如下面提到的,我可能需要添加正确的标题。旁注:我必须在变量字符串的开头添加一个“?”(所有URL传递的变量的典型字符)以使连接正常工作。这与我直接使用浏览器时得到的响应不同。您的浏览器会发送一组标题。使用Fiddler或Firebug嗅探浏览器发送的HTTP请求。然后使用client.headers属性复制这些头。
result += "<script>self.document.forms[0].submit()</script>";
someModel.rawHTML = result;
return View(someModel);
@model SomeModel
@{
Layout = null;
}
@Html.Raw(@Model.rawHTML)
protected void Button1_Click(object sender, EventArgs e)
{
var formPostText = @"<html><body><div>
<form method=""POST"" action=""OtherLogin.aspx"" name=""frm2Post"">
<input type=""hidden"" name=""field1"" value=""" + TextBox1.Text + @""" />
<input type=""hidden"" name=""field2"" value=""" + TextBox2.Text + @""" />
</form></div><script type=""text/javascript"">document.frm2Post.submit();</script></body></html>
";
Response.Write(formPostText);
}