- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个我一直在开发的 winforms 应用程序,它对消费者帐户运行多个测试。测试需要一次性登录才能执行。
string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text;
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
requestLogin.Method = "POST";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.ContentLength = paramaters.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(paramaters);
stOut.Close();
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
此脚本适用于登录,问题是当我需要实际运行测试时,我需要将所有结果返回到一个字符串中(HTML 结果)。
private string runTestRequest(Uri url, string parameters)
{
string testResults = string.Empty;
HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
runTest.CookieContainer = cookieJar;
runTest.Method = "POST";
runTest.ContentType = "application/x-www-form-urlencoded";
StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(parameters);
stOut.Close();
StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream());
testResults = stIn.ReadToEnd();
stIn.Close();
return testResults;
}
但是它会尝试登录。我如何将之前登录请求中的 Cookie 与此以及许多其他 Web 请求一起使用?
感谢您的帮助。
编辑:
我将它添加到我的代码中,但它应该做与 BrokenGlass 所说的相同的事情,只是有点不同但仍然不起作用。
foreach (Cookie cookie in responseLogin.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
最佳答案
这样的事情应该可行,我正在使用类似的代码来保存登录 cookie:
HttpWebRequest runTest;
//...do login request
//get cookies from response
CookieContainer myContainer = new CookieContainer();
for (int i = 0; i < Response.Cookies.Count; i++)
{
HttpCookie http_cookie = Request.Cookies[i];
Cookie cookie = new Cookie(http_cookie.Name, http_cookie.Value, http_cookie.Path);
myContainer.Add(new Uri(Request.Url.ToString()), cookie);
}
//later:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.url.com/foobar");
request.CookieContainer = myContainer;
关于使用 Cookie 的 C# WebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4158448/
我正在查看 WebRequest.Create 之间的文档和 WebRequest.CreateHttp来决定我应该在我的应用程序中使用哪个。我没有看到这两种方法之间有什么不同,除了 WebReque
我想知道 DefaultWebProxy 和 GetSystemWebProxy() 之间的主要区别是什么。 MSDN 上有一些描述,但我仍然觉得我需要更多细节才能更好地理解。 此外,假设我在 C#
我想在 UI 上显示下载正在进行时下载文件。使用 WebRequest 我有两个选择: 使用 WebRequest.BeginGetResponse 和相关方法。 优点:可以以字节为单位显示准确的进度
我正在尝试注销对 MonoTouch 库的引用,所使用的是: IWebProxy oDefaultProxy = CFNetwork.GetDefaultProxy (); 在 .NET 中,还有
我首先发布了这个:HttpWebRequest: How to find a postal code at Canada Post through a WebRequest with x-www-fo
在 Windows PowerShell 3.0 中引入了 Invoke-RestMethod小命令。 Invoke-RestMethod cmdlet 接受 -Body用于设置请求正文的参数。 由于
我正在尝试在.bat文件中下载一个exe,但是无法隐藏输出 $progressPreference = 'silentlyContinue'| Out-Null 和$null 2>&1. 我不知道为什
我正在尝试 curl 包含分号的有效负载,但它失败了 Invoke-WebRequest -Uri $uri -Body '{"text" : "foo;"}' -Method Post Invoke
我有一个带有状态页面的网络设备,该页面可以通过Java小程序访问。使用Fiddler,我可以找到状态的http提要,但是页面会不断刷新。 (Firefox会显示该页面,但会保持刷新状态,Chrome会
有人可以向我解释为什么cUrl(真正的网址)有效但Invoke-WebRequest无效吗?同一台机器,相同变量。在我看来,他们俩都应该做同样的事情,将文件上传到jfrog Artifactory。
我正在尝试在 WebRequest 中发送表单数据。 该函数工作正常并按预期返回“成功”响应流。 但是,如果“data”变量的长度超过 30,000 个字符,我会遇到 HTTP 500 错误: Mes
我正在尝试在 WebRequest 中发送表单数据。 该函数工作正常并按预期返回“成功”响应流。 但是,如果“data”变量的长度超过 30,000 个字符,我会遇到 HTTP 500 错误: Mes
下面的代码需要怎么修改才能发送WebRequest通过指定的 proxy server和 port number ? Dim Request As HttpWebRequest = WebReques
我想执行一堆 WebRequest,但设置了可以同时启动的阈值。 我遇到了这个LimitedConcurrencyTaskScheduler example并尝试像这样使用它 scheduler =
我使用下面的代码在线检查一些pdf文件并相应地返回一个字符串。 问题是:当我添加第二个 Task.Factory.StartNew() 时,它开始复制所有请求,但仍然只返回一个答案(应该如此)。 我需
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseurl + url); req.Timeout = 1000 * 10;
我正在使用 WebRequest 执行简单请求,应用程序只是挂起,直到响应返回。我该如何解决这个问题? 看了很多题目,都说要用线程。我不知道如何使用它们;任何人都可以提供以下不挂起用户界面的示例吗?
以下代码在 WinForms 和 Windows Phone 8 应用程序中返回错误。 代码 var jsonData = "jsonStringGoesHere"; var uri
我正在用 C# 构建一个网络抓取工具,用于处理代理和大量请求。页面通过 ConnectionManager 类加载,该类获取代理并使用随机代理重试加载该页面,直到页面被正确加载。 平均一个任务需要10
我在做一个项目,多次调用一个网址 request = (HttpWebRequest)WebRequest.Create(url); request.GetResponse(); 这是我的代码。
我是一名优秀的程序员,十分优秀!