Tag: httpwebrequest

尝试使用HttpWebRequest获取身份validationcookie

我必须从安全站点刮一张桌子,我无法登录页面并检索身份validation令牌和任何其他相关的cookie。 我在这里做错了吗? public NameValueCollection LoginToDatrose() { var loginUriBuilder = new UriBuilder(); loginUriBuilder.Host = DatroseHostName; loginUriBuilder.Path = BuildURIPath(DatroseBasePath, LOGIN_PAGE); loginUriBuilder.Scheme = “https”; var boundary = Guid.NewGuid().ToString(); var postData = new NameValueCollection(); postData.Add(“LoginName”, DatroseUserName); postData.Add(“Password”, DatrosePassword); var data = Encoding.ASCII.GetBytes(postData.ToQueryString(false)); var request = WebRequest.Create(loginUriBuilder.Uri) as HttpWebRequest; request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = data.Length; using (var […]

有没有办法在ASP.NET MVC 3 RC2中禁用JSON ModelBinder?

在ASP.NET MVC 3 RC2中,如果Content-Type设置为application/json ,则默认的ModelBinder将自动解析请求主体。 问题是,这会在流的末尾留下Request.InputStream 。 这意味着如果您尝试使用自己的代码读取输入流,则首先将其重置为开头: // client sends HTTP request with Content-Type: application/json and a JSON // string in the body // requestBody is null because the stream is already at the end var requestBody = new StreamReader(Request.InputStream).ReadToEnd(); // resets the position back to the beginning of the input stream var reader […]

如何计算HttpWebRequest花费的出站和入站互联网流量

我有以下function来获取页面。 我的问题是我想计算一下互联网连接的数量 入站(下载)和出站流量(已发送) 我怎样才能做到这一点 ? 谢谢 我的function public static string func_fetch_Page(string srUrl, int irTimeOut = 60, string srRequestUserAgent = “Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0”, string srProxy = null) { string srBody = “”; string srResult = “”; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(srUrl); request.Timeout = irTimeOut * 1000; request.UserAgent = srRequestUserAgent; request.KeepAlive […]

如何使用winforms从网上下载显示下载进度的图像?

我使用WebRequest以这种方式异步下载URL中的图像: public void Download(string url) { byte[] buffer = new byte[0x1000]; WebRequest request = HttpWebRequest.Create(url); request.Method = “GET”; request.ContentType = “image/gif”; request.BeginGetResponse(result => { WebRequest webRequest = result.AsyncState as WebRequest; WebResponse response = webRequest.EndGetResponse(result); ReadState readState = new ReadState() { Response = response.GetResponseStream(), AccumulatedResponse = new MemoryStream(), Buffer = buffer, }; readState.Response.BeginRead(buffer, 0, readState.Buffer.Length, ReadCallback, […]

如何获得快速的.Net Http请求

我需要一个Http请求,我可以在.Net中使用它需要不到100毫秒。 我能够在浏览器中实现这一点,所以我真的不明白为什么这在代码中是个问题。 我已经尝试过WinHTTP以及WebRequest.Create,它们都超过500毫秒,这对我的用例来说是不可接受的。 以下是我试图通过的简单测试的示例。 (WinHttpFetcher是我写的一个简单的包装器,但是它确实是一个获取请求的最简单的例子,我不确定它是否值得粘贴。) 我正在使用LibCurlNet获得可接受的结果,但如果同时使用该类,我会收到访问冲突。 此外,由于它不是托管代码,必须复制到bin目录,因此使用我的开源项目进行部署并不理想。 尝试另一个实现的任何想法? [Test] public void WinHttp_Should_Get_Html_Quickly() { var fetcher = new WinHttpFetcher(); var startTime = DateTime.Now; var result = fetcher.Fetch(new Uri(“http://localhost”)); var endTime = DateTime.Now; Assert.Less((endTime – startTime).TotalMilliseconds, 100); } [Test] public void WebRequest_Should_Get_Html_Quickly() { var startTime = DateTime.Now; var req = (HttpWebRequest) WebRequest.Create(“http://localhost”); var response = req.GetResponse(); var […]

如何在不下载内容的情况下执行GET请求?

我正在研究一个链接检查器,一般来说我可以执行HEAD请求,但是有些网站似乎禁用了这个动词,所以在失败时我还需要执行一个GET请求(仔细检查链接是否真的死了) 我使用以下代码作为我的链接测试器: public class ValidateResult { public HttpStatusCode? StatusCode { get; set; } public Uri RedirectResult { get; set; } public WebExceptionStatus? WebExceptionStatus { get; set; } } public ValidateResult Validate(Uri uri, bool useHeadMethod = true, bool enableKeepAlive = false, int timeoutSeconds = 30) { ValidateResult result = new ValidateResult(); HttpWebRequest request = WebRequest.Create(uri) as […]

System.Net.WebException:操作已超时

我有一个大问题:我需要一次发送200个对象并避免超时。 while (true) { NameValueCollection data = new NameValueCollection(); data.Add(“mode”, nat); using (var client = new WebClient()) { byte[] response = client.UploadValues(serverA, data); responseData = Encoding.ASCII.GetString(response); string[] split = Javab.Split(new[] { ‘!’ }, StringSplitOptions.RemoveEmptyEntries); string command = split[0]; string server = split[1]; string requestCountStr = split[2]; switch (command) { case “check”: int requestCount = Convert.ToInt32(requestCountStr); […]

在C#.Net中使用HttpWebRequest异步上传大文件

我想使用HttpWebRequest以异步方式上传大文件。 假设我使用HttpWebRequest上传音频文件&在接收端,它应该获取流并播放音频文件。 是否有任何代码或示例可用于ref。 ? 如果有任何例子,请提供。 提前致谢

c# – 使用Cookie的WebRequest HTTP POST(来自curl脚本的端口)

IBM RTC RESTful api提供了一个用于向服务器进行身份validation的shell脚本示例: COOKIES=./cookies.txt USER=my_user PASSWORD=my_password HOST=”https://myJazzServer:9092/jazz” curl -k -c $COOKIES “$HOST/authenticated/identity” curl -k -L -b $COOKIES -c $COOKIES -d j_username=$USER -d j_password=$PASSWORD “$HOST/authenticated/j_security_check” 这非常有效,但我需要使用c#对服务器进行身份validation。 到目前为止,我有以下内容,但它无法正常工作(返回授权失败页面): CookieContainer _cookie; public string _RTC() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://myJazzServer.com:9092/jazz/authenticated/identity”); if (_cookie == null) { _cookie = new CookieContainer(); } string a; request.CookieContainer = _cookie; using (var response […]

使用HttpWebRequest在POST中使用C#Escape Plus Sign(+)

我在发送POST数据时遇到问题,包括密码字段中的“+”字符, string postData = String.Format(“username={0}&password={1}”, “anyname”, “+13Gt2”); 我正在使用HttpWebRequest和webbrowser来查看结果,当我尝试使用HttpWebRequest从我的C#WinForms 登录到POST数据到网站时,它告诉我密码不正确。 (在源代码[richTexbox1]和webBrowser1中)。 尝试使用我的另一个帐户,它不包含’+’字符,它允许我正确登录(使用字节数组并将其写入流) byte[] byteArray = Encoding.ASCII.GetBytes(postData); //get the data request.Method = “POST”; request.Accept = “text/html”; request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = byteArray.Length; Stream newStream = request.GetRequestStream(); //open connection newStream.Write(byteArray, 0, byteArray.Length); // Send the data. newStream.Close(); //this works well if user does not includes symbols 从这个问题我发现HttpUtility.UrlEncode()是逃避非法字符的解决方案,但我无法找到如何正确使用它,我的问题是,在使用urlEncode()对我的POST数据进行url编码之后我该怎么办?正确地将数据发送给我的请求 ? […]