Tag: httpwebrequest

Uri.EscapeDataString() – 无效的URI:Uri字符串太长

我在Windows Mobile上使用紧凑框架/ C#。 在我的应用程序中,我通过序列化对象并使用HttpWebRequest / POST请求将数据上传到服务器来上传数据。 在服务器上,后期数据被反序列化并保存到数据库中。 前几天我意识到我在post数据(&符号等)中遇到了特殊字符的问题。 所以我在方法中引入了Uri.EscapeDataString(),一切都很顺利。 但是,今天我发现当应用程序尝试上传大量数据时会出现问题(我不确定此刻究竟表示“大”的是什么!) 现有代码(种类) var uploadData = new List(); uploadData.Add(new Thing() { Name = “Test 01” }); uploadData.Add(new Thing() { Name = “Test 02” }); uploadData.Add(new Thing() { Name = “Test with an & Ampersand ” }); // Do this a lot!! var postData = “uploadData=” + Uri.EscapeDataString(JsonConvert.SerializeObject(uploadData, […]

C#中的套接字:如何获取响应流?

我想替换这个: void ProcessRequest(object listenerContext) { var context = (HttpListenerContext)listenerContext; Uri URL = new Uri(context.Request.RawUrl); HttpWebRequest.DefaultWebProxy = null; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL); httpWebRequest.Method = context.Request.HttpMethod; httpWebRequest.Headers.Clear(); if (context.Request.UserAgent != null) httpWebRequest.UserAgent = context.Request.UserAgent; foreach (string headerKey in context.Request.Headers.AllKeys) { try { httpWebRequest.Headers.Set(headerKey, context.Request.Headers[headerKey]); } catch (Exception) { } } using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) { Stream […]

C#HttpWebRequest SEC_I_RENEGOTIATE间歇性错误

我正在使用C#(。Net framework 3.5)应用程序中的SSL POST调用来处理登录/注销function。 通过HttpWebRequest :: BeginGetResponse()从服务器获取响应的工作时间为80%,但另外20%是间歇性抛出: The request was aborted: Could not create SSL/TLS secure channel. 我使用另一个问题中的建议文章启用了SSL跟踪。 这在请求跟踪中产生了两种不同的模式。 似乎在执行期间,错误: System.Net Error: 0 : [3680] Decrypt returned SEC_I_RENEGOTIATE. 正在接收,导致重新启动安全上下文。 当发生这种情况并且成功时,这是输出(注意我省略了实际地址): System.Net Error: 0 : [3680] Decrypt returned SEC_I_RENEGOTIATE. System.Net Information: 0 : [3680] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 4bec0d0:4c0a8a8, targetName = [omitted].com, inFlags = ReplayDetect, […]

抛出WebException但永远不会被抓住

我有以下代码: try { using (var myHttpWebResponse = (HttpWebResponse) httPrequestCreated.GetResponse()) { var streamResponse = myHttpWebResponse.GetResponseStream(); if (streamResponse != null) { var streamRead = new StreamReader(streamResponse); var readBuff = new Char[256]; var count = streamRead.Read(readBuff, 0, 256); while (count > 0) { var outputData = new String(readBuff, 0, count); finalResopnse += outputData; count = streamRead.Read(readBuff, 0, 256); […]

是什么让这个HTTPS WebRequest即使在浏览器中运行也会超时?

这是我的要求: var request = (HttpWebRequest) WebRequest.Create(“https://mtgox.com/”); request.CookieContainer = new CookieContainer(); request.AllowAutoRedirect = false; request.Accept = “text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”; request.Headers[HttpRequestHeader.AcceptEncoding] = “gzip, deflate”; request.Headers[HttpRequestHeader.AcceptLanguage] = “en-gb,en;q=0.5”; request.Headers[HttpRequestHeader.AcceptCharset] = “ISO-8859-1,utf-8;q=0.7,*;q=0.7”; request.Timeout = 5000; request.UserAgent = “Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0”; request.Method = “GET”; request.GetResponse(); 使用HttpFox从Firefox复制标题。 我使用Fiddler2来validation至少对于HTTP请求,标头在Firefox请求和我的请求之间是完全相同的。 但是,当使用HTTPS 对此特定网站执行请求时,请求会超时。 它适用于其他网站。 我必须以与Firefox不同的方式执行它,因为它始终适用于Firefox。 但是,我无法使用Fiddler2调试它,因为每当Fiddler2转发这些请求时,它们也会超时 ,即使是由Firefox发起的。 它只是一个非常错误的网站吗? 上面的哪一部分让我离开了不是Firefox?

在Parallel.For中发送多个WebRequest

我想发送多个WebRequest 。 我使用Parallel.For循环来做到这一点,但循环运行一次,第二次在获得响应时出错。 错误: 操作已超时 代码: Parallel.For(0, 10, delegate(int i) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create( new Uri(“http://www.mysite.com/service”)); string dataToSend = “Data”; byte[] buffer = System.Text.Encoding.GetEncoding(1252). GetBytes(dataToSend); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = buffer.Length; request.Host = “www.mysite.com”; Stream requestStream = request.GetRequestStream(); requestStream.Write(buffer, 0, buffer.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); });

为什么我的HttpWebRequest返回400 Bad请求?

以下代码因400错误请求exception而失败。 我的网络连接很好,我可以去网站,但我不能用HttpWebRequest得到这个uri。 private void button3_Click(object sender, EventArgs e) { WebRequest req = HttpWebRequest.Create(@”http://www.youtube.com/”); try { //returns a 400 bad request… Any ideas??? WebResponse response = req.GetResponse(); } catch (WebException ex) { Log(ex.Message); } }

为什么我的Http客户端在指定凭据时发出2个请求?

我创建了RESTful webservice(WCF),我检查每个请求的凭据。 我的一个客户是Android应用程序,服务器端的一切似乎都很棒。 我收到请求,如果它有正确的标题 – 我处理它等。 现在我创建了使用此服务的客户端应用程序。 这是我做GET的方式: // Create the web request var request = WebRequest.Create(Context.ServiceURL + uri) as HttpWebRequest; if (request != null) { request.ContentType = “application/json”; // Add authentication to request request.Credentials = new NetworkCredential(Context.UserName, Context.Password); // Get response using (var response = request.GetResponse() as HttpWebResponse) { // Get the response stream […]

C# – 从403错误中获取响应正文

从URL请求数据时,我收到403错误。 这是预期的,我不会问如何纠正它。 将此URL直接粘贴到我的浏览器中时,我会获得一个基本信息串,用于描述拒绝权限的原因。 我需要通过我的C#代码读取这个基本错误消息,但是当发出请求时,System.Net.WebException(“远程服务器返回错误:(403)Forbidden。”)抛出错误,并且响应正文我无法使用。 是否可以简单地抓取页面的内容而不抛出exception? 相关的代码几乎是你所期望的,但无论如何它都在这里。 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sPageURL); try { //The exception is throw at the line below. HttpWebResponse response = (HttpWebResponse)(request.GetResponse()); //Snipped processing of the response. } catch(Exception ex) { //Snipped logging. } 任何帮助,将不胜感激。 谢谢。

为什么这个WebRequest代码会变慢?

我要求100页,全部404.我写了 { var s = DateTime.Now; for(int i=0; i < 100;i++) DL.CheckExist("http://google.com/lol" + i.ToString() + ".jpg"); var e = DateTime.Now; var d = es; d=d; Console.WriteLine(d); } static public bool CheckExist(string url) { HttpWebRequest wreq = null; HttpWebResponse wresp = null; bool ret = false; try { wreq = (HttpWebRequest)WebRequest.Create(url); wreq.KeepAlive = true; wreq.Method = […]