Tag: httpwebrequest

使用HttpWebRequest收到截断的响应

我正在使用以下代码将HttpWebRequests创建到一个网站: public static HttpWebResponse SendGETRequest(string url, string agent, CookieContainer cookieContainer) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = agent; request.Method = “GET”; request.ContentType = “text/html”; request.CookieContainer = cookieContainer; return (HttpWebResponse)request.GetResponse(); } 几个网页都运行良好,直到我尝试使用新网页并且只收到页面的最后一部分。 这是收到的回复: 标题是正确的,并表示只发送接收的数据。 以下是请求和响应的标头: 请求: GET /Broker/Ops/FichaContratoJS.asp?nc=815044&IP=5&YY=2012&M=6&St=0&CC=FESX201206 HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19 Content-Type: text/html Host: www.xxxx.com Cookie: […]

我可以阻止Uri在WebRequest.Create中取消编码吗?

代码本身并不复杂,它只是不能正常工作: Uri uri = new Uri(“https://www.google.com/webmasters/tools/feeds/sites/http%3A%2F%2Fwww.mydomain.co.uk%2F”); WebRequest.Create(uri); 我从服务器上得到一个“错误请求”,经过多次挖掘,发现Uri正在变成 https://www.google.com/webmasters/tools/feeds/sites/http%3A//www.mydomain.co.uk/ 这不是我要求的,所以它有一个问题 有办法阻止这个吗?

使用没有服务引用的HttpWebRequest动态使用Web服务

我工作的应用程序必须将数据发送到外部系统。 系统系统将有一个Web服务(c#或java或php),我需要使用它。 由于将有与客户端一样多的外部系统,我需要将WSDL文件,方法名称和参数作为用户输入并将数据发送到外部系统。 所以,我试图使用此处提供的代码动态调用Web服务 我在这里测试了很少的免费网络服务 我从WSDL文件中找到SOAP位置,方法名称和参数,并给出相同的输入。 以下服务按预期工作 http://soaptest.parasoft.com/calculator.wsdl Location – http://ws1.parasoft.com/glue/calculator Method Name – add Parameter – x,y 但是,当我通过提供SOAP位置和方法名称为另一个免费服务尝试相同时,它会抛出500内部服务器错误。 http://www.predic8.com:8080/crm/CustomerService?wsdl Location – http://www.predic8.com:8080/crm/CustomerService Method Name – getAll 我通过在soapUI中测试上面的wsdl来确认这些输入是正确的。 soapUI请求窗口中使用相同的位置。 我不确定为什么会抛出错误。 请帮我理解。 另请告诉我是否可以从WSDL文件获取服务位置并使用HttpWebRequest获取响应。 我担心这种调用Web服务的方法是否会起作用,而不管用于实现Web服务的技术如何。 编辑: 问题似乎与SOAP信封有关。 对于http://soaptest.parasoft.com/calculator.wsdl,即使我们忽略xmlns:cal =“http://www.parasoft.com/wsdl/calculator/,它也会成功执行。 但对于http://www.predic8.com:8080/crm/CustomerService?wsdl ,如果我忽略xmlns:ns =“http://predic8.com/wsdl/crm/CRMService/1/,则会引发内部服务器错误。 请分享我在这里如何通用

如何使HttpWebRequest异步

我有这样的代码: private async Task Request(url) { Task task = null; try { task = MakeAsyncRequest(url, “text/html”); return await task; } catch { return null; } } private async Task MakeAsyncRequest(string url, string contentType) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = contentType; request.Method = WebRequestMethods.Http.Get; request.Timeout = 20000; request.Proxy = null; Task task = Task.Factory.FromAsync( request.BeginGetResponse, […]

无法处理从HTTP / HTTPS协议重定向到其他不同的协议

基本上,我正试图从CNet的Download.com上获取EXE 所以我创建了Web解析器,到目前为止一切顺利。 以下是从其网站直接提取的示例链接: http://dw.com.com/redir?edId=3&siteId=4&oId=3001-20_4-10308491&ontId=20_4&spi=e6323e8d83a8b4374d43d519f1bd6757&lop=txt&tag=idl2&pid=10566981&mfgId=6250549&merId=6250549&pguid=PlvcGQoPjAEAAH5rQL0AAABv&destUrl=ftp%3A%2F%2F202.190.201.108%2Fpub%2Fryl2%2Fclient%2Finstaller-ryl2_v1673.exe 问题出在这里:当您尝试下载时,它以HTTP开头,然后重定向到FTP站点。 我已经尝试过.NET的WebClient和HttpWebRequest对象,看起来两者都不能支持重定向。 此代码在GetResponse()时失败; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(“http://dw.com.com/redir”); WebResponse response = req.GetResponse(); 现在,我也试过这个: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(“http://dw.com.com/redir”); req.AllowAutoRedirect = false; WebResponse response = req.GetResponse(); string s = new StreamReader(response.GetResponseStream()).ReadToEnd(); 并且它不再抛出错误,但变量s变成空字符串。 我不知所措! 任何人都可以帮忙吗?

为什么使用“新的NetworkCredential(用户名,密码)”不适用于我的网站的基本身份validation(来自WinForms C#应用程序)?

我有一个使用基本身份validation(用户名/密码)的网站。 为什么以下代码不起作用? 当我运行它时,Web应用程序将我带到登录控制器,而我期望它应该已经过身份validation,因为我正在填充凭据。 换句话说,我正在尝试确认如何在.NET中确认我的winforms HttpWebRequest,以便它将自动化身份validation过程。 我假设NetworkCredential是应该这样做的.net类? 或者在.NET中是否期望有一些手动两步过程你必须自己实现? 这是代码: // Create a new webrequest to the mentioned URL. var uri = new Uri(“http://10.1.1.102:3000/download/sunset”); var myWebRequest = (HttpWebRequest)WebRequest.Create(uri); myWebRequest.PreAuthenticate=true; var networkCredential=new NetworkCredential(“test”, “asdfasdf”); myWebRequest.Credentials=networkCredential; var myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); Console.Out.WriteLine(“STATUS = ” + myWebResponse.StatusCode); var stream = myWebResponse.GetResponseStream(); var reader = new StreamReader(stream); string text_read = reader.ReadToEnd(); Console.WriteLine(text_read); […]

如何在MVC 3中的Request.Form集合中访问未经validation的项目

我在.NET 4.0中使用ASP.NET MVC 3。 我有一个模型,其中一个属性要求允许HTML内容。 我在我的模型属性上放置了AllowHtml属性,该属性允许在该属性上使用HTML。 这本身就有效。 我也在我网站的其他部分使用Uploadify flash上​​传程序。 由于flash和会话的问题 ,我使用的代码类似于swfupload示例中的代码,以允许我的文件上传访问会话数据。 基本上我直接在Application_BeginRequest处理程序中访问Request.Form集合。 我遇到的问题是,当提供HTML的表单被提出时,当Application_BeginRequest处理程序中的代码访问Request.Forms[key]集合时,我得到一个HttpRequestValidationException 。 就像我在开始时说的那样,我尝试过AllowHtml属性。 我还尝试使用ValidateInput(false)属性在动作和控制器级别禁用validation,但我相信我在请求生命周期中对于那些要应用的人来说太早了。 无论如何都要访问包含“潜在危险”数据的Request.Form集合而不禁用整个站点的请求validation?

底层连接在HttpWebRequest POST上关闭在生产服务器上

我得到了“底层连接已关闭:连接意外关闭。” 尝试使用生产服务器上的HttpWebRequest类进行POST时出错,在我的开发机器上运行正常。 我最初尝试使用WebClient类,但我转而使用HttpWebRequest来尝试我在研究问题时找到的一些建议(例如将KeepAlive设置为false,将PreAuthenticate设置为true,将ProtocolVersion设置为1.0)。 由于它只发生在生产服务器上,我猜它可能与IIS有关。 这是我的代码 HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(webURL); ASCIIEncoding encoding=new ASCIIEncoding(); Byte[] postbytes = Encoding.ASCII.GetBytes(data); HttpWReq.Headers.Add(“Authorization”, String.Format(“Basic {0}”, authstring)); HttpWReq.KeepAlive = false; HttpWReq.PreAuthenticate = true; HttpWReq.Credentials = CredentialCache.DefaultCredentials; HttpWReq.UseDefaultCredentials = true; HttpWReq.ProtocolVersion = HttpVersion.Version10; HttpWReq.Method = “POST”; HttpWReq.ContentType = “application/x-www-form-urlencoded”; HttpWReq.ContentLength = postbytes.Length; Stream newStream = HttpWReq.GetRequestStream(); newStream.Write(postbytes, 0, postbytes.Length); newStream.Close(); 我最初尝试使用WebClient类的代码 /* WebClient […]

C#REST API调用 – 在邮递员中工作,而不是在代码中工作

我有一些现有的代码正在工作,并且突然退出。 我无法弄清楚为什么…… 这是我的代码: public static string RequestToken(string u, string pw) { string result = string.Empty; string strUrl = “https://xxx.cloudforce.com/services/oauth2/token?grant_type=password&client_id=XXXX&client_secret=XXXX&username=” + u + “&password=” + pw; HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest; Debug.Print(strUrl); tokenRequest.Method = “POST”; try { using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse) { if (tokenResponse.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format( “Server error (HTTP […]

检测您的操作系统在C#中安装了哪个版本的IE?

我目前正在管理一个Windows服务,该服务以编程方式生成HttpWebRequest和HttpWebResponse对象,以检索请求的响应消息。 HttpWebRequest的UserAgent属性被硬编码为使用IE 6作为浏览器代理。 是一种通过程序方式检测托管服务的服务器上安装的IE版本的方法吗? 它目前托管在Windows Server 2003计算机上,可能安装在Windows Server 2008计算机上。