0

I am trying to access a page where I need to log into the website and pass a a list of parameters. I appear to be able to log into the site (if I change the log in details I get a 401 unauthorised error) but then I get a 400 bad request error. The code is a bit hashed together so I know something is wrong but don't know where.

EDITED CODE

        Public Sub TestConn()


    Dim customeremailaddress As String = HttpUtility.UrlEncode("[email protected]")
    Dim customername As String = HttpUtility.UrlEncode("Ryan")
    Dim referenceid As String = HttpUtility.UrlEncode("ordertest123")
    Dim languagecode As String = HttpUtility.UrlEncode("1043")
    Dim expirydays As String = HttpUtility.UrlEncode("30")

    Dim UserName As String = "testusername"
    Dim password As String = "testpassword"
    Dim siteCredentials As New NetworkCredential(UserName, password)

    Dim URLAuth As String = "http://service.someurl.com/process.xml"
    Dim postString As String = String.Format("customeremailaddress={0}&customername={1}&referenceid={2}&languagecode={3}&expirydays={4}", customeremailaddress, customername, referenceid, languagecode, expirydays)

    Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postString)

    Const contentType As String = "application/x-www-form-urlencoded"
    System.Net.ServicePointManager.Expect100Continue = False

    Dim cookies As New CookieContainer()
    Dim webRequest__1 As HttpWebRequest = TryCast(WebRequest.Create(URLAuth), HttpWebRequest)
    webRequest__1.Method = "POST"
    webRequest__1.ContentType = contentType
    webRequest__1.CookieContainer = cookies
    webRequest__1.ContentLength = postBytes.Length
    webRequest__1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"
    webRequest__1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    webRequest__1.Referer = "http://service.someurl.com/process.xml"
    webRequest__1.Credentials = siteCredentials

    Try
        Dim requestStream As Stream = webRequest__1.GetRequestStream()
        requestStream.Write(postBytes, 0, postBytes.Length)
        Dim responseReader As New StreamReader(webRequest__1.GetResponse().GetResponseStream())
        Dim responseData As String = responseReader.ReadToEnd()
        responseReader.Close()
        webRequest__1.GetResponse().Close()

    Catch ex As Exception
        Lbl_ConnTest_error.Text = ex.Message
    End Try

End Sub
13
  • Id start by using Fiddler2 fiddler2.com/fiddler2 to have a look at the request/response details and see if that points you in the right direction. Commented Mar 26, 2013 at 9:36
  • Also check out this example msdn.microsoft.com/en-gb/library/debx8sh9.aspx - it might be easier to read - also as its POSTing data dont forget to properly format the postData i.e. url encode the value part and create key value pairs i.e. variable1=Some%20value&variable2=Another%20Value Commented Mar 26, 2013 at 9:41
  • I think this is where I may be getting the bad request as I don't know what the syntax is for actually passing the list of parameters. I guessed at - Dim postString As String = String.Format("customeremailaddress={0}&customername={1}&referenceid={2}&languagecode={3}&expirydays={4}", customeremailaddress, customername, referenceid, languagecode, expirydays) - but I'm sure this doesn't look right? Commented Mar 26, 2013 at 9:57
  • You need to combine @Sani 's answer with wrapping HttpServerUtility.UrlEncode() around your values i.e. Dim customeremailaddress As String = HttpServerUtility.UrlEncode("[email protected]") Commented Mar 26, 2013 at 11:04
  • You are now writing the bytes TWICE. This is why the content length doesn't match. You do not need the StreamWriter. Remove all code in regards of the requestWriter object. As bUKaneer says, you also need to UrlEncode the values in the query string. Commented Mar 27, 2013 at 11:39

1 Answer 1

1

You need to send the bytes of the postString not the string itself:

Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postString)
...
webRequest__1.ContentLength = postBytes.Length
...
Dim requestStream As Stream = webRequest__1.GetRequestStream()
requestStream.Write(postBytes, 0, postBytes.Length)

More information here.

Sign up to request clarification or add additional context in comments.

4 Comments

I have made those changes but now get (400) Bad Request on the line - Dim responseReader As New StreamReader(webRequest__1.GetResponse().GetResponseStream()) - near the end.
I have tidied the code a little and am now getting: Bytes to be written to the stream exceed the Content-Length bytes size specified. Something to do with the UTF8 encoding?
Check that when you are setting the ContentLength you are specifying postBytes.Length and not postString.Length. Also check that when you Write to the stream you are specifying the postBytes.Length property.
Have edited the original post with the complete code for the Content-length bytes issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.