如何从HttpWebRequest的一个pasteing URL到地址栏不同(功能)? [英] How does HttpWebRequest differ (functional) from pasteing a URL into an address bar?

查看:195
本文介绍了如何从HttpWebRequest的一个pasteing URL到地址栏不同(功能)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我缩小到 2 prior 问题。

基本上,我有一个网址当我手动把它拿来(粘贴到浏览器)工作得很好,但是当我通过一些代码运行(使用HttpWebRequest)有一个不同的结果。

Basically, I've got a URL that when I fetch it manually (paste it into browser) works just fine, but when I run through some code (using the HttpWebRequest) has a different result.

的URL(示例):


http://208.106.250.207:8192/announce?info_hash=-%CA8%C1%C9rDb%ADL%ED%B4%2A%15i%80Z%B8%F%C&peer_id=01234567890123456789&port=6881&uploaded=0&downloaded=0&left=0&compact=0&no_peer_id=0&event=started

该代码:

String uri = BuildURI(); //Returns the above URL
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Proxy = new WebProxy();
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
... Parse the result (which is an error message from the server claiming the url is incorrect) ...

所以,我怎样才能从给定的URL的服务器得到什么?我明明在这里做得不对,但又说不出什么。

So, how can I GET from a server given a URL? I'm obviously doing something wrong here, but can't tell what.

无论是对我的代码修复,或实际工作就可以了另一种方法。我不是在所有HttpWebRequest方法结婚。

Either a fix for my code, or an alternative approach that actually works would be fine. I'm not wed at all to the HttpWebRequest method.

推荐答案

那么,他们可能会有所不同的只是在HTTP标头需要发送。尤其是用户代理。

Well, the only they might differ is in the HTTP headers that get transmitted. In particular the User-Agent.

另外,你为什么要使用WebProxy?这是不是真的有必要,它很可能不使用你的浏览器。

Also, why are you using a WebProxy? That is not really necessary and it most likely is not used by your browser.

你的代码的其余部分是很好..只要确保你正确设置HTTP头。检查此链接出来:

The rest of your code is fine.. Just make sure you set up the HTTP headers correctly. Check this link out:

我建议你让自己的是Wireshark 并检查您的浏览器,并且您试图访问服务器之间发生的通信。这样做将是相当琐碎使用Wireshark的,它会告诉你,是从浏览器发送的确切HTTP消息。

I would suggest that you get yourself a copy of WireShark and examine the communication that happens between your browser and the server that you are trying to access. Doing so will be rather trivial using WireShark and it will show you the exact HTTP message that is being sent from the browser.

再看看这那张通信你的C#应用​​程序和服务器(再次使用Wireshark的),然后之间比较两个以找出究竟是不同的。

Then take a look at the communication that goes on between your C# application and the server (again using WireShark) and then compare the two to find out what exactly is different.

如果通信是一个纯HTTP GET方法(即不存在涉及HTTP消息体)和URL是正确的,那么唯一的两件事我能想到的是:

If the communication is a pure HTTP GET method (i.e. there is no HTTP message body involved), and the URL is correct then the only two things I could think of are:


  1. 确保你是发出正确的协议(即HTTP / 1.0或HTTP / 1.1或不管它是什么,你应该发送)

  2. 请确保您正确地发送所有必需的HTTP头,和很明显,你是不是送了你不应该发送任何HTTP标头。

这篇关于如何从HttpWebRequest的一个pasteing URL到地址栏不同(功能)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆