Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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...
}


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):
VB
- 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


Thanks, for any suggestion.


EDIT:
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.
Posted
Updated 30-Jan-12 22:21pm
v2
Comments
lynkx.vavri 30-Jan-12 8:59am    
Correction: webRequest should be of HttpWebRequest type:

var webRequest = (HttpWebRequest)WebRequest.Create(requestURL);
Sergey Alexandrovich Kryukov 30-Jan-12 14:17pm    
You are basically right, but OP does not use any methods of HttpWebRequest, so in this simple case this is just fine.
--SA

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:

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


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
 
Share this answer
 
Comments
NeptuneHACK! 30-Jan-12 16:57pm    
my 5 ;D
Sergey Alexandrovich Kryukov 30-Jan-12 17:05pm    
Thank you.
--SA
lynkx.vavri 31-Jan-12 3:55am    
Thanks, but System.Net.WebRequest.Create method doesn't support relative URIs:

{"This operation is not supported for a relative URI."}

I also tryied to create requestUrl using 'baseUrl' and 'relativeUrl'
new Uri(baseUri, relativeUri);
, hoping to tell webRequest to use only relative part later on, but webRequest.RequestUri.IsAbsoluteUri is readonly property. Kinda don't don't understand why is it there.
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:

C#
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...
}



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[^]
 
Share this answer
 
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[^]
 
Share this answer
 
Comments
lynkx.vavri 31-Jan-12 4:11am    
My app is system service used for automated data download with database import. I don't think that System.Windows.Navigation lib is exactly what I need, but I'll take a look at it just for getting out of the box. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900