[英]Using HttpWebRequest to POST to a form on an outside server
I am trying to simulate a POST to a form on an external server that does not require any authentication, and capture a sting containing the resulting page. 我试图模拟POST到外部服务器上不需要任何身份验证的表单,并捕获包含结果页面的sting。 This is the first time I have done this so I am looking for some help with what I have so far.
这是我第一次这样做,所以我正在寻找一些帮助,我到目前为止。 This is what the form looks like:
这就是表单的样子:
<FORM METHOD="POST" ACTION="/controller" NAME="GIN">
<INPUT type="hidden" name="JSPName" value="GIN">
Field1:
<INPUT type="text" name="Field1" size="30"
maxlength="60" class="txtNormal" value="">
</FORM>
This is what my code looks like: 这就是我的代码:
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Field1=VALUE1&JSPName=GIN";
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://XXX/controller");
myRequest.Method = "POST";
myRequest.ContentType = "text/html";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
StreamReader reader = new StreamReader(newStream);
string text = reader.ReadToEnd();
MessageBox.Show(text);
newStream.Close();
Currently, the code returns "Stream was not readable". 目前,代码返回“Stream is not readable”。
You want to read the Response stream: 您想要阅读Response流:
using (var resp = myRequest.GetResponse())
{
using (var responseStream = resp.GetResponseStream())
{
using (var responseReader = new StreamReader(responseStream))
{
}
}
}
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Field1=VALUE1&JSPName=GIN";
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://XXX/");
myRequest.Method = "POST";
myRequest.ContentType = "text/html";
myRequest.ContentLength = data.Length;
string result;
using (WebResponse response = myRequest.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.