-1

I keep getting an error saying 'Invalid URI: The format of the URI could not be determined.' The error happens on line 3
This is my code:

public static string Get()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"27.**.***.14");

            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            string result = readStream.ReadToEnd();
            response.Close();
            readStream.Close();
            return result;
        }

Edit: it turns out that I was missing a http:// in front of the IP address, doing this solved the issue

cir
  • 35
  • 1
  • 1
  • 10

2 Answers2

1

try putting the protocol prefix in front the ip, i.e. in your case "http://"

-3

I keep getting an error saying 'Invalid URI: The format of the URI could not be determined.' This is my code:

Let me start with saying most of your code is totally irrelevant. See, the problem is with the string you put in which happens in line 1:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"27..*.14");

The rest is noise. And you funny enough never even bother to tell us at which line the error happens.

And also the IP points to a HTTP server I have running on a raspberry PI

That is ALSO irrelevant. Because the parsing happens without even contacting the server there is no requirement for there actually BEING a server.

This:

.Create(@"27..*.14");

is (assuming ** is a valid number each) is NOT A URL, period. It is an IP address. URL's are not IP Addresses - among other things they must specify a protocol. Yes, one could assume that the software should assume - like your browser. It just is not implemented.

So, next time:

  • Go through the code step by step. You have a debugger for a reason. This would allow you to cut down the post with removing all the lines that never ever execute.
  • Read the error message. It is quite clear that it tells you this is not a URL.
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 3
    what a "super useful" and "non-hateful" post, and not only that, but you never even gave an answer. – cir May 26 '21 at 07:32