1

Am trying to create a simple page that connects to an external website, logs in, and then passes a bunch of parameters.

When I run the page I get a bad request 400 error and when I check using Fiddler I can see there are both 401 and 400 errors being returned. Under 'Auth' in Fiddler I see:

"No Proxy-Authorization Header is present. No Authorization Header is present." < Is this relevant? As when I test using PHP cUrl the page can log in fine and Fiddler says the same under Auth.

    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 testcapture As String = requestStream.ToString
        Dim thing As String = "stop"

        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   

Seems the login credentials are not being passed what am I doing wrong? I am obviously using vb in an asp.net application but am connecting to an XML file held on Linux Apache, does this have any implications at all?

Fiddler appears to be doing strange things for me now so if anyone else can see anything using Fiddler a link to the test file is here: http://www.filemanage.co.uk/Pubs/Default.aspx

Agamemnon
  • 587
  • 2
  • 15
  • 44
  • First things first. Can you browse to the website in a browser? – CathalMF Mar 28 '13 at 09:36
  • I can browse to the page just fine, and when I do a login box appears. If I enter the correct details the xml page is shown that I am passing parameters to. – Agamemnon Mar 28 '13 at 09:39
  • Could be the content type is being rejected by the server. – CathalMF Mar 28 '13 at 09:45
  • Hey Ryan - sorry you're still plugging away at this - check the accepted answer on this post for properly specifying basic auth ;o) http://stackoverflow.com/questions/1683398/passing-networkcredential-to-httpwebrequest-in-c-sharp-from-asp-net-page – Luke Baughan Mar 28 '13 at 09:55
  • Thanks @bUkaneer, it's one of those things I guess. I just have to keep at it and listen to the many helpful peeps on here such as yourself. I'll take a look at that answer now. EDIT just noticed I have read this answer (along with many others) to no avail... oh well – Agamemnon Mar 28 '13 at 09:58
  • Try to mimic what exactly you see in Fiddler when using web browser by your application. Providing Fiddler output when using browser and you application helps. – Xaqron Mar 28 '13 at 10:07
  • Hmmm OK what about this http://stackoverflow.com/questions/2764577/forcing-basic-authentication-in-webrequest it manually adds the header rather than using the networkcredentials object? – Luke Baughan Mar 28 '13 at 10:09
  • Well, I was going to try the things suggested above but Fiddler seems to have gone nuts on me. It now won't capture information from my local test application and when I use the files on my remote test server it just shows the page as being loaded successfully. Rather than returning what the end server said (400/401 etc) it returns a 200 as in the page is fine that I created that has a label to show caught exceptions (which is indeed showing the 401). I have added a link to the remote test page I'm using in the main post if anyone has fiddler and can see anything obvious. – Agamemnon Mar 28 '13 at 11:07

0 Answers0