c# - HttpWebRequest 序列

标签 c# .net http web httpwebrequest

我目前正在自动化管理 FTP 用户的 Web 界面。

我正在尝试使用 HttpWebRequest 执行此操作,我有一个调用让我登录该站点,第二个调用应该为 FTP 访问添加一个新用户。

我在浏览器中尝试了我的两个 url,它们都有效,它们最终创建了一个用户。

string login = "https://www.ftpsite.net/./panel/index.php?txvUsername=myaccount&txvPassword=myPassword&submitButton=Login";

当我在浏览器地址栏中输入它时,这会让我登录。

创建用户的第二次调用如下。

string createUser = "https://www.ftpSite.net/panel/ftpsites/updatelogin?login_id=&login=toto123&realname=realnametoto&homedir=root&passwd=superpassword11&expdate=01-01-2100&neverExpire=on&quota_value=0&quota_unit=GB&group_id=0&status=on&ftp=on&filelist=on&ftp_download=on&http=on&web_filelist=on&web_download=on&email=";

当我在浏览器的地址栏中输入它时,如果它跟在我们登录的地址之后,就会创建一个用户。

我的问题是,我尝试使用 HttpWebRequest 执行此操作但没有成功。我可以让自己登录,但是当我尝试创建用户时,它似乎返回一个“错误”错误代码,说我已经创建了太多用户,但事实并非如此,因为我可以在该调用后创建更多用户。这是我与 HtttpRequest 一起使用的代码

        _datCookie = new CookieContainer();
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(login);
        httpWebRequest.Method = "POST";
        httpWebRequest.CookieContainer = _datCookie;

        WebResponse response = httpWebRequest.GetResponse();
        referer = response.ResponseUri.AbsoluteUri;

        Stream requestStream = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(requestStream);
        _datCookie = httpWebRequest.CookieContainer;
        response.Close();



        httpWebRequest = (HttpWebRequest)WebRequest.Create(createUser);
        httpWebRequest.CookieContainer = _datCookie;
        httpWebRequest.Referer = referer;
        httpWebRequest.Method = "POST";


        response = httpWebRequest.GetResponse();

        requestStream = response.GetResponseStream();
        streamReader = new StreamReader(requestStream);


        webBrowser.DocumentText = streamReader.ReadToEnd();

        response.Close();

我捕获并试图模仿但没有成功的东西here .

最佳答案

你确定它们应该是 POST 请求吗? URL 似乎包含查询字符串中的所有字段,这表明它们应该是 GET 请求。

根据 Fiddler 屏幕截图,您需要使用主体中的字段而不是查询字符串发出 POST 请求:

var cookies = new CookieContainer();

// Request 1 : Login
var request = (HttpWebRequest)WebRequest.Create("https://www.ftpsite.net/./panel/index.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookies;

string postData = "txvUsername=myaccount&txvPassword=myPassword&submitButton=Login";
byte[] postBytes = Encoding.Default.GetBytes(postData);
request.ContentLength = postBytes.Length;
using (Stream bod = request.GetRequestStream())
{
   body.Write(postBytes, 0, postBytes.Length);
}

WebResponse response = request.GetResponse();
string referer = response.ResponseUri.AbsoluteUri;

// Request 2 : Create user
request = (HttpWebRequest)WebRequest.Create("https://www.ftpSite.net/panel/ftpsites/updatelogin");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookies;

postData = "login_id=&login=toto123&realname=realnametoto&homedir=root&passwd=superpassword11&expdate=01-01-2100&neverExpire=on&quota_value=0&quota_unit=GB&group_id=0&status=on&ftp=on&filelist=on&ftp_download=on&http=on&web_filelist=on&web_download=on&email=";
postBytes = Encoding.Default.GetBytes(postData);
request.ContentLength = postBytes.Length;
using (Stream bod = request.GetRequestStream())
{
   body.Write(postBytes, 0, postBytes.Length);
}

response = request.GetResponse();
requestStream = response.GetResponseStream();
streamReader = new StreamReader(requestStream);
webBrowser.DocumentText = streamReader.ReadToEnd();
response.Close();

关于c# - HttpWebRequest 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13309804/

相关文章:

javascript - Angular 2 不显示来自 HTTP 的数据

c# - 在多个集合中使用同一个锁对象是线程安全的吗?

.net - 覆盖 Equals 方法时是否需要覆盖 == 和 != 运算符? (。网)

c# - 每 0.1 毫秒和 0.3 毫秒重复两个任务(Systems.Timer.Timer 不够精确)

http - Microsoft Edge重定向http ://localhost to https://localhost

http - 设计 REST 服务时请求正文内容类型中使用的自定义媒体类型?

c# - 如何在 Windows 中获取和设置系统音量

c# - 在 F# 中使用绑定(bind)接口(interface)

c# - DataContractSerialiser 和 Serializable 的问题

.net - 如何模拟 'out' 参数?