Tag: httpwebrequest

在WinRT和C#中使用HttpRequestHeaders

我正在使用HttpWebRequests联系API并需要添加标头,但编译器告诉我该方法不存在。 然而,当我检查MSDN时,它告诉我该方法已经存在。 设置我的UserAgent属性也会失败。 有人可以帮我吗? try{ HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url); wr.Method = “GET”; wr.Headers.Add(System.Net.HttpRequestHeader.Authorization, string.Format(“Bearer {0}”, _accessToken)); wr.UserAgent = _appNameAndContact; var resp = (System.Net.HttpWebResponse) await wr.BetterGetResponse(); if (resp.StatusCode == System.Net.HttpStatusCode.OK) { using (var sw = new System.IO.StreamReader(resp.GetResponseStream())) { var msg = sw.ReadToEnd(); User usr = JsonConvert.DeserializeObject(msg); //var results = JSONHelper.Deserialize(msg); return usr; } } }

以编程方式将数据发布到Web表单时出现乱码的httpWebResponse字符串

我试图搜索之前关于这个问题的讨论,但我找不到一个,也许是因为我没有使用正确的关键字。 我正在编写一个小程序,将数据发布到网页上并获得响应。 我发布数据的网站没有提供API。 经过一些谷歌搜索后,我开始使用HttpWebRequest和HttpWebResponse。 代码如下所示: HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(“https://www.site.com/index.aspx”); CookieContainer cookie = new CookieContainer(); httpRequest.CookieContainer = cookie; String sRequest = “SomeDataHere”; httpRequest.Accept = “text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”; httpRequest.Headers.Add(“Accept-Encoding: gzip, deflate”); httpRequest.Headers.Add(“Accept-Language: en-us,en;q=0.5”); httpRequest.Headers.Add(“Cookie: SomecookieHere”); httpRequest.Host = “www.site.com”; httpRequest.Referer = “https://www.site.com/”; httpRequest.UserAgent = “Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1”; httpRequest.ContentType = “application/x-www-form-urlencoded”; //httpRequest.Connection = “keep-alive”; httpRequest.ContentLength = […]

在C#中保存来自Webrequest的图像

我正在使用jQuery网络摄像头插件与我的页面中的网络摄像头进行通信并拍摄快照。 它的工作方式是与Flash助手通信。 要保存图片,它将获取另一个页面的名称,并向该页面发送Web请求。 我成功地收到了另一个请求。 我想保存该请求中的图像。

序列化XmlDocument并通过HTTPWebRequest发送

我正在试图弄清楚如何正确序列化我的XmlDocument并通过HTTPWebRequest对象发送它。 这是我到目前为止所拥有的: Stream requestStream; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://wwwcie.ups.com/ups.app/xml/Track”); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = postData.Length; requestStream = request.GetRequestStream(); XmlSerializerNamespaces xsm = new XmlSerializerNamespaces(); xsm.Add(“”, “”); // remove namespace XmlSerializer ser = new XmlSerializer(xmlRequest.GetType()); ser.Serialize(requestStream, xmlRequest); requestStream.Write(postData, 0, postData.Length); requestStream.Close(); 我不确定的一些事情。 我需要在同一个HTTPWebRequest中发送2个XmlDocuments。 我之前尝试过将XmlDocuments转换为字符串并将它们连接起来(发送字符串)但是当我使用StringBuilder / Writer时它会添加: 我已经在我的XmlDocument对象中有声明,所以现在它在那里两次,我不能在那里有<string…部分。 是否更容易将XmlDocuments转换为字符串然后连接它们并发送它或是否有一种简单的方式来发送XmlDocuments? 编辑: 请参阅C#XmlDocument节点当我尝试将我的一个XmlDocuments转换为字符串时,它显示为 whatever 123 我希望我的root是

如何从重定向的URL下载文件?

我正在尝试从不包含该文件的链接下载文件,而是重定向到包含实际文件的另一个(临时)链接。 目标是获得程序的更新副本,而无需打开浏览器。 链接是: http://www.bleepingcomputer.com/download/minitoolbox/dl/65/ 我试过使用WebClient,但它不起作用: private void Button1_Click(object sender, EventArgs e) { WebClient webClient = new WebClient(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); webClient.DownloadFileAsync(new Uri(“http://www.bleepingcomputer.com/download/minitoolbox/dl/65/”), @”C:\Downloads\MiniToolBox.exe”); } 在搜索并尝试了许多事情之后,我发现了这个涉及使用HttpWebRequest.AllowAutoRedirect的解决方案。 通过具有重定向的代码下载文件? // Create a new HttpWebRequest Object to the mentioned URL. HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(“http://www.contoso.com”); myHttpWebRequest.MaximumAutomaticRedirections=1; myHttpWebRequest.AllowAutoRedirect=true; HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse(); 这似乎正是我正在寻找的,但我根本不知道如何使用它:/我猜这个链接是WebRequest.Create的参数。 但是如何将文件检索到我的目录? 是的,我是noob …先谢谢你的帮助。

如何像HttpWebRequest一样用TcpClient做HTTPS?

我有一个基于TcpClient的通信系统,除了它对特定IP做HTTPS之外,它工作得很好。 然后它开始失败。 通过使用浏览器或HttpWebRequest,我可以毫无问题地为该IP做HTTPS。 我已经创建了一个测试程序来将我的问题缩小到基本的本质,如果你需要,你可以在这里查看它: TestViaTcp 该测试程序完全适用于基本HTTP到同一IP,它总是产生对请求的成功响应。 我把它放在一个循环中,用按键触发它,它将一整天继续成功。 一旦我切换HTTPS,我就会得到一个反复出现的模式。 它会工作,然后就不会,成功之后是失败,然后整天来回成功。 我一直遇到的特殊失败是这样的: {“Authentication failed because the remote party has closed the transport stream.”} [System.IO.IOException]: {“Authentication failed because the remote party has closed the transport stream.”} Data: {System.Collections.ListDictionaryInternal} HelpLink: null InnerException: null Message: “Authentication failed because the remote party has closed the transport stream.” Source: “System” TargetSite: {Void […]

Discord添加公会成员401错误尽管显然有效访问令牌

我是Discord API的新手,我正在开发一个项目,需要能够以编程方式添加一个公会成员。 我已经学会了如何获取授权代码(带有identify和guilds.join范围),将其兑换为访问令牌,并获取用户的ID。 最后一步是使用访问代码和用户ID来添加公会。 此命令详述如下: https://discordapp.com/developers/docs/resources/guild#add-guild-member 我似乎需要向此URL发送PUT请求: https://discordapp.com/api/guilds/[GuildID]/members/[UserID] 但这导致了这种反应: {“code”:0,“message”:“401:未经授权”} 我尝试在Authorization标头中包含访问令牌: 授权:持票人[编辑] 我还尝试在请求中添加JSON主体: { “的access_token”: “[删除]”} 两者都没有奏效。 不出所料,同时使用两者并没有奏效。 我想知道这是否是权限问题,但Discord确认我有guilds.join范围。 这是我在交换访问令牌的授权码时收到的JSON: {“access_token”:“[Redacted]”,“token_type”:“Bearer”,“expires_in”:604800,“refresh_token”:“[Redacted]”,“scope”:“识别guilds.join”} 识别范围有效,因为我能够检索用户及其ID。 但是guilds.join似乎不起作用。 我在下面提供了一些测试代码。 我已经标记了“选项1”和“选项2”行,以表示我通常不会在同一请求中执行这两种访问代码方法。 但正如我之前提到的,我确实尝试了两种方法,但仍然有401错误。 using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.ContentType, “application/x-www-form-urlencoded”); client.Headers.Add(HttpRequestHeader.Authorization, “Bearer [Redacted]”);//Option 1 string output = client.UploadString ( “https://discordapp.com/api/guilds/[GuildID]/members/[UserID]”, WebRequestMethods.Http.Put, “{\”access_token\”:\”[Redacted]\”}”//Option 2 ); } 因为我想了解其工作方式的内在性,我宁愿知道如何使用普通的Web请求(例如HttpWebRequest和WebClient,而不是使用某些OAuth库)。

HttpWebRequest响应产生HTTP 422.为什么?

我正在尝试以编程方式向Web服务器发送POST请求,以便登录然后执行其他需要登录的请求。 这是我的代码: byte[] data = Encoding.UTF8.GetBytes( String.Format( “login={0}&password={1}&authenticity_token={2}” +”&login_submit=Entra&remember_me=1″, HttpUtility.UrlEncode(username), HttpUtility.UrlEncode(password), HttpUtility.UrlEncode(token))); //Create HTTP-request for login HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(“http://www.xxx.xx/xx/xx”); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = data.Length; request.CookieContainer = new CookieContainer(); request.Accept = “application/xml,application/xhtml+xml,text/html; +”q=0.9,text/plain ;q=0.8,image/png,*/*;q=0.5”; request.Referer = “http://www.garzantilinguistica.it/it/session”; request.Headers.Add(“Accept-Language”, “de-DE”); request.Headers.Add(“Origin”, “http://www.xxx.xx”); request.UserAgent = “C#”; request.Headers.Add(“Accept-Encoding”, “gzip, deflate”); 发送请求后 //Send post request […]

传输编码:在Windows Phone中分块

我有一个使用Transfer-Encoding的服务器响应:chunked HTTP/1.1 200 OK Server: nginx/1.2.1 Date: Mon, 18 Feb 2013 08:22:49 GMT Content-Type: application/json; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding c7 {} 0 在json数据之前查看c7块大小。 如何使用HttpWebResponse在Windows Phone中读取没有块的原始响应流? 提示:要使服务器禁用分块输出,我只需要指定HTTP / 1.0协议版本。 但我不知道怎么做,因为Windows Phone或Silverlight中的HttpWebRequest类中没有ProtocolVersion属性

是否可以使用HttpWebRequest更改标题顺序?

我需要更改标题的顺序,我正在使用它: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.Method = context.Request.HttpMethod; request.UserAgent = context.Request.UserAgent; 输出是: GET /* HTTP/1.1 User-Agent: My Server Host: 127.0.0.1:1080 但它应该是 GET /* HTTP/1.1 Host: 127.0.0.1:1080 User-Agent: My Server 有任何想法吗? 谢谢你的时间。 编辑:也许有一种方法使用其他对象……它也是一种选择