Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C完成表单操作后下载源代码网页_C# - Fatal编程技术网

C# 用C完成表单操作后下载源代码网页

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

例如: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;

        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中包含查询字符串。