VB.NET Post REST HTTP request without http in baseaddress

bizantd
bizantd used Ask the Experts™
on
I have vb.net api which requires to connect to HOST with out using http.For example its using serverbase address="someipwithout http".

I am getting this error:{"Invalid URI: The URI scheme is not valid."}

However if I use wfetech it works fine.

My Code is as follow

Dim serverAddress = "someip:customport"
Dim URI As String = "/some uri"

Dim client As New HttpClient(handler)
client.BaseAddress = New Uri(serverAddress)
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/xml"))
Dim response = client.PostAsXmlAsync(URI, x).Result()
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you are not using the http: protocol, what protocol are you using?
bizantdEngineer

Author

Commented:
I am using Http protocol.
Most Valuable Expert 2012
Top Expert 2008

Commented:
System.Uri needs to know the protocol, so does this work?

Dim serverAddress = "http://someip:customport"
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

bizantdEngineer

Author

Commented:
Hi Bob
Syntactically it works but.I am also using wfetch to validate the response from the server.
If I use Dim serverAddress="www.microsoft.com" its giving the error but in wfetch it works fine on http
and this is same what I want to achieve.
Most Valuable Expert 2012
Top Expert 2008

Commented:
Well, that is a conundrum, since you can't use System.Uri without a protocol, and I believe that you need System.Uri here:

client.BaseAddress = New Uri(serverAddress)

Open in new window


You would need to find a different approach that doesn't require a Uri.
Engineer
Commented:
Hi Bob

I worked it out by using

Dim GRequest = DirectCast(WebRequest.Create(ServerAddressUri), HttpWebRequest)
Most Valuable Expert 2012
Top Expert 2008

Commented:
Is ServerAddressUri a Uri or string variable?
bizantdEngineer

Author

Commented:
Its a URI and gives me same result as if I was using without http.
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you constructing the Uri the same as here:

client.BaseAddress = New Uri(serverAddress)
bizantdEngineer

Author

Commented:
yes I am.
Most Valuable Expert 2012
Top Expert 2008

Commented:
That doesn't make any sense, but oh well :D
bizantdEngineer

Author

Commented:
Dim GRequest = DirectCast(WebRequest.Create(ServerAddressUri), HttpWebRequest)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial