1

我目前正在自动化管理 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 执行此操作但没有成功。我可以让自己登录,但是当我尝试创建用户时,它似乎返回一个“错误”错误代码,说我已经创建了太多用户,但事实并非如此,因为我可以在该调用之后创建更多用户。这是我与 HttpRequest 一起使用的代码

        _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();

我在这里捕捉并试图模仿但没有成功。

4

1 回答 1

1

您确定它们应该是 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();
于 2012-11-12T20:23:05.697 回答