通过HttpWebRequest上的边界完成字段
本文关键字:字段 边界 HttpWebRequest 通过 | 更新日期: 2023-09-27 18:00:30
我正试图通过C#应用程序的HttpWebRequest登录到一个网站。登录页面的帖子数据看起来像:
-----------------------------18327245165630'r'n
Content-Disposition: form-data; name="user"'r'n
'r'n
123'r'n
-----------------------------18327245165630'r'n
Content-Disposition: form-data; name="pass"'r'n
'r'n
1234'r'n
-----------------------------18327245165630'r'n
Content-Disposition: form-data; name="mode"'r'n
'r'n
login'r'n
-----------------------------18327245165630'r'n
Content-Disposition: form-data; name="submit"'r'n
'r'n
Submit'r'n
-----------------------------18327245165630--'r'n
字段为user和pass,在浏览器上完成的值为123以及1234。
问题是如何通过HttpWebRequest发送这些值?
我的代码看起来像:
private void loginButton_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest oRequest = null;
oRequest = (HttpWebRequest)HttpWebRequest.Create(loginLinkPost);
oRequest.ContentType = "multipart/form-data; boundary=" + PostData.boundary;
oRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0";
oRequest.KeepAlive = true;
oRequest.AllowAutoRedirect = true;
oRequest.Method = "POST";
oRequest.CookieContainer = cookies;
PostData pData = new PostData();
Encoding encoding = Encoding.UTF8;
System.IO.Stream oStream = null;
pData.Params.Add(new PostDataParam("email", this.usernameBox.Text, PostDataParamType.Field));
pData.Params.Add(new PostDataParam("password", this.passwordBox.Text, PostDataParamType.Field));
pData.Params.Add(new PostDataParam("mode", "login", PostDataParamType.Field));
pData.Params.Add(new PostDataParam("submit", "Submit", PostDataParamType.Field));
byte[] buffer = encoding.GetBytes(pData.GetPostData());
oRequest.ContentLength = buffer.Length;
oStream = oRequest.GetRequestStream();
oStream.Write(buffer, 0, buffer.Length);
oStream.Close();
string sursa = "";
using (HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse())
{
if (oResponse.StatusCode == HttpStatusCode.OK)
{
System.IO.Stream responseStream = oResponse.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
sursa = reader.ReadToEnd();
MessageBox.Show("Logged in :)!");
}
else
{
setStatus("Server response failed...");
MessageBox.Show("Server failed to respond!", "Error!");
setStatus("Ready...");
}
}
}
catch(Exception ex)
{
setStatus("Error while trying to log in...");
MessageBox.Show(ex.ToString(), "Error when try to login...");
setStatus("Ready...");
}
}
PostData.cs类:
namespace IPSocksBot
{
public class PostData
{
public static string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
private List<PostDataParam> m_Params;
public List<PostDataParam> Params
{
get { return m_Params; }
set { m_Params = value; }
}
public PostData()
{
m_Params = new List<PostDataParam>();
}
public string GetPostData()
{
StringBuilder sb = new StringBuilder();
foreach (PostDataParam p in m_Params)
{
sb.AppendLine("--" + boundary + "''r''n");
if (p.Type == PostDataParamType.File)
{
sb.AppendLine(string.Format("Content-Disposition: file; name='"{0}'"; filename='"{1}'"", p.Name, p.FileName));
sb.AppendLine("Content-Type: application/octet-stream");
sb.AppendLine();
sb.AppendLine(p.Value);
}
else
{
sb.AppendLine(string.Format("Content-Disposition: form-data; name='"{0}'"''r''n", p.Name));
sb.AppendLine("''r''n");
sb.AppendLine(p.Value + "''r''n");
}
}
sb.AppendLine("--" +boundary + "--" + "''r''n");
return sb.ToString();
}
}
public enum PostDataParamType
{
Field,
File
}
public class PostDataParam
{
public PostDataParam(string name, string value, PostDataParamType type)
{
Name = name;
Value = value;
Type = type;
}
public PostDataParam(string name, string filename, string value, PostDataParamType type)
{
Name = name;
Value = value;
FileName = filename;
Type = type;
}
public string Name;
public string FileName;
public string Value;
public PostDataParamType Type;
}
}
我缺少什么?为什么我的价值观没有提交?我用Fiddler来查看我的发送帖子值,它们完全相同。
当我看到您的代码时,我注意到有两件事可能会导致问题
第一:您应该使用相同的名称发送数据
如果您看到浏览器发送用户,您也应该使用它。因此,将电子邮件更改为用户,并对传递字段执行相同操作
-----------------------------18327245165630'r'n
Content-Disposition: form-data; name="user"'r'n
'r'n
123'r'n
所以你的代码应该像这样:
pData.Params.Add(new PostDataParam("user", this.usernameBox.Text, PostDataParamType.Field));
pData.Params.Add(new PostDataParam("pass", this.passwordBox.Text, PostDataParamType.Field));
pData.Params.Add(new PostDataParam("mode", "login", PostDataParamType.Field));
pData.Params.Add(new PostDataParam("submit", "Submit", PostDataParamType.Field));
我注意到的第二件事是在你的PostData.cs类中
您应该输入'r'n
而不是''r''n
。
请确保您的请求长度等于fiddler发送的请求长度
希望它能帮助你。顺致敬意,