C# 用C完成表单操作后下载源代码网页
例如:https://www.website.com/index.php?action=get_products 此页面/操作具有以下源代码:Table php页面有以下源代码:Hello 当我使用以下代码时,仍然会得到index.php代码,而不是操作页面代码:C# 用C完成表单操作后下载源代码网页,c#,C#,例如:https://www.website.com/index.php?action=get_products 此页面/操作具有以下源代码:Table php页面有以下源代码:Hello 当我使用以下代码时,仍然会得到index.php代码,而不是操作页面代码: Uri url = new Uri("https://www.website.com/index.php"); HttpWebRequest request = null; ServicePointM
Uri url = new Uri("https://www.website.com/index.php");
HttpWebRequest request = null;
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
CookieContainer cookieJar = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookieJar;
request.Method = "GET";
HttpStatusCode responseStatus;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
responseStatus = response.StatusCode;
url = request.Address;
}
if (responseStatus == HttpStatusCode.OK)
{
UriBuilder urlBuilder = new UriBuilder(url);
urlBuilder.Path = urlBuilder.Path.Remove(urlBuilder.Path.LastIndexOf('/')) + "/j_security_check";
request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
request.Referer = url.ToString();
request.CookieContainer = cookieJar;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (Stream requestStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
{
string postData = "?action=get_products";
requestWriter.Write(postData);
}
string responseContent = null;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader responseReader = new StreamReader(responseStream))
{
responseContent = responseReader.ReadToEnd();
}
Console.WriteLine(responseContent);
}
else
{
Console.WriteLine("Client was unable to connect!");
}
我在stackoverflow线程中找到了上面的代码。问题是我必须先用用户名和密码登录。如何使用C完成此操作?为什么要标记此php?因为您试图访问的html是由PHP生成的,所以您的代码不应该关心html是如何生成的。请指定网站支持的身份验证协议。您的PHP是否处理不同操作的POST方法或GET方法?你的第一句话建议得到。如果它应该是后,然后首先删除它?从您的帖子数据中,接下来将两行提要附加到帖子中,最后在流编写器上调用Flush。如果该方法应该是GET,那么只需在请求Uri中包含查询字符串。