Link to home
Start Free TrialLog in
Avatar of Lynchie435
Lynchie435

asked on

400 Bad Request with HTTP GET Request

Hi,

I am pretty new to web services as a whole and I'm trying to access and return data from a web service for a data project.

Everytime I send the Web Request I am receiving a 400 Bad Request message, now I am unsure if this is their end or mine. Is there any chance someone can look over this code and tell me if there is anything wrong out of the blue?

I was told to send the following headers in my request:

'Accept', 'Accept-Encoding','Authorization','Content-Length','Date'.

Now to my knowledge I have covered this in my code below yet I still receive a 400 Bad Request.

Dim _auth As String = String.Format("{0}:{1}", "user", "pass")
        Dim _enc As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth))
        Dim _cred As String = String.Format("{0} {1}", "Basic ", _enc)

        Dim getVars As String = "?type=Q&dln0=ABCDYAZ123&ind0=P&pc=BS23+1DE&"

        Dim WebReq As HttpWebRequest = DirectCast(WebRequest.Create(String.Format("https://myurl/this", getVars)), HttpWebRequest)

        'This time, our method is GET.
        WebReq.Method = "GET"
        WebReq.Accept = "application/json"
        WebReq.ContentLength = 0
        WebReq.AutomaticDecompression = DecompressionMethods.None
        WebReq.Headers(HttpRequestHeader.Authorization) = _cred
        WebReq.Date = Date.UtcNow

        'From here on, it's all the same as above.
        Dim WebResp As HttpWebResponse = DirectCast(WebReq.GetResponse(), HttpWebResponse)
        'Let's show some information about the response
        Console.WriteLine(WebResp.StatusCode)
        Console.WriteLine(WebResp.Server)

        'Now, we read the response (the string), and output it.
        Dim Answer As Stream = WebResp.GetResponseStream()
        Dim _Answer As New StreamReader(Answer)
        Console.WriteLine(_Answer.ReadToEnd())

Open in new window


Please can someone point me in the right direction into my failure as I'm struggling to see what is wrong.

Regards,

James
ASKER CERTIFIED SOLUTION
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The server's log should give you more information.  If you don't have access to the log, then I would suggest you get hold of somebody that does.
Avatar of Lynchie435
Lynchie435

ASKER

That helped me out mate, Muchos!