具有相对网址的Http WebRequest [英] Http WebRequest with relative url

查看:64
本文介绍了具有相对网址的Http WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有谁知道是否仍然存在以下限制? :

{无效的URI:无法确定URI的格式."}

...当尝试像这样重新创建http GET请求时:

Hi,
does anyone know whether there is anyway to get around following limitation? :

{"Invalid URI: The format of the URI could not be determined."}

...when trying to re-create http GET request like this:

private static DataTable GetWebpage(string requestURL, string pathname)
{
var webRequest = WebRequest.Create(requestURL);
webRequest.Method = "GET";
webRequest.Proxy = new WebProxy(proxy, proxyPort);
// header parameters settings

StreamReader streamIn = 
           new StreamReader(webRequest.GetResponse().GetResponseStream());                     
string response = streamIn.ReadToEnd();
streamIn.Close();
 
//response parsing and data storage...
}



诀窍是目标服务器将不接受绝对地址,而是将我重定向到错误页面.我需要以下格式的Http标头,但WebRequest不会让我(System.Net.WebClient出现相同的问题):



The trick is that destination server won''t accept absolute address and redirects me to error page. I need Http header in following format, but WebRequest won''t let me(the same problem is with System.Net.WebClient):

- Http:
    Command: GET
  - URI: requestUrl(with params)
     Location: requestUrl(without params)
   + Parameters: 0x1
    ProtocolVersion: HTTP/1.1
// header parameters settings
    Host:  proxy:proxyPort
    Connection:  Keep-Alive
    HeaderEnd: CRLF



谢谢您的任何建议.



发现有趣的事情,当我尝试通过提琴手的本地http代理获取getResponse时,请求的http标头实际上是固定的(URL的基本部分已删除).

注意:我正在使用"Microsoft网络监视器"实用程序来监听传出的流量.



Thanks, for any suggestion.



Found out interesting thing, when I try to getResponse through fiddler''s local http proxy, request''s http header is actualy fixed (base part of url is removed).

NOTE: I''m sniffing outgoing traffic with ''Microsoft Network Monitor'' utility.

推荐答案

这是因为您正在使用System.Net.WebRequest.Create(string),而您更需要System.Net.WebRequest.Create(System.Uri).

您正在处理默认情况下从字符串创建的无效URI,因为URL是相对的,但默认情况下假定它是绝对的.从明确指示System.UriKind.Relative:
的字符串构造Uri
This is because you are using System.Net.WebRequest.Create(string) where you rather need System.Net.WebRequest.Create(System.Uri).

You are dealing with invalid URI created by default from string, because URL is relative, but by default is is assumed it was absolute. Construct your Uri from string explicitly indicating System.UriKind.Relative:

var webRequest =
    System.Net.WebRequest.Create(new System.Uri(
          requestUrl,
          System.UriKind.Relative //it should solve the problem
    ));



请参阅:
http://msdn.microsoft.com/en-us/library/system.net. webrequest.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0aa3d588.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.uri.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms131565.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.urikind.aspx [ ^ ].

-SA



Please see:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^],
http://msdn.microsoft.com/en-us/library/0aa3d588.aspx[^],
http://msdn.microsoft.com/en-us/library/system.uri.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131565.aspx[^],
http://msdn.microsoft.com/en-us/library/system.urikind.aspx[^].

—SA


好,为了使生成的http标头看起来不像我需要告诉
的代理请求 它不应该连接以设置代理的连接管理类:

Ok, in order for generated http header not to look like proxy request I needed to tell to
connection management class that it should not connect to set up proxy:

private static DataTable GetWebpage(string requestURL, string pathname)
{
var webRequest = WebRequest.Create(requestURL);
webRequest.Method = "GET";
webRequest.Proxy = new WebProxy(proxy, proxyPort);

FieldInfo field_ServicePoint_ProxyServicePoint = (typeof(ServicePoint))
.GetField("m_ProxyServicePoint", BindingFlags.NonPublic | BindingFlags.Instance);
field_ServicePoint_ProxyServicePoint.SetValue(webRequest.ServicePoint, false);

// header parameters settings

StreamReader streamIn = 
           new StreamReader(webRequest.GetResponse().GetResponseStream());                     
string response = streamIn.ReadToEnd();
streamIn.Close();
 
//response parsing and data storage...
}




我还必须启用unsafeHeaderparsing,因为目标服务器违反了
标头格式作为响应,但出现此异常:

{服务器违反了协议.Section = ResponseStatusLine"}

我在 msdn论坛中找到了以编程方式解决此问题的解决方案a> [ ^ ]




I also had to enable unsafeHeaderparsing because target server violated
header format in response and I got this exception:

{"The server committed a protocol violation. Section=ResponseStatusLine"}

I found solution for programatically solving this on msdn forum[^]


之前也曾提出过类似的问题(对于银色灯,但您仍然使用规定的步骤).这些讨论可能加起来更多:

如何导航到Project外部的页面在Silverlight中 [ ^ ]
A similar question is asked earlier (for silver light, but you still use the stated steps). These discussions may add up more :

How to Navigate to A page outside Project in Silverlight[^]


这篇关于具有相对网址的Http WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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