472,958 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

HttpWebRequest POST result is not the same as POST via ServerXMLHT

I am attempting to post to a url (https://FakeURL/logon.asp) using the
HttpWebRequest class. The response for a succesful post will contain
the html for the logon user's default page.

We've accomplished this in the past utilizing the ServerXMLHTTP object. When
I try an equivalent? post utilizing the HttpWebRequest class,
the response contains the html for logon.asp (the same page that was posted
to), which indicates to me that in some way my post didn't work.
No exceptions are thrown, it's just that the wrong page is returned.

The request is through a proxy server to the internet, via https. I've set
the headers the same way as with the successful call through
ServerXMLHTTP. I've tried it both with setting and not setting the
Credentials property of the request. In setting Credentials, I've tried
using a CredentialCache, or just a NetworkCredential

The code for both the ServerXMLHTTP, and the .Net page is below. Can anyone
tell me what I'm missing?

Thanks,
Dave Brown


'Dot Net Code
-------------------------------------------------------------------------------------------

Public Class WebForm1

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sUID As String = "xxxxx"
Dim sPassword As String = "xxxxx"
Dim strURL As String = "https://FakeURL/logon.asp"
Dim strRetval As String
strRetval = getPage(strURL, "Mode=&Username=" & sUID & "&Password="
& sPassword)
Response.Write(strRetval)
End Sub
Public Shared Function getPage(ByVal url As String, ByVal payload As
String) As String
Dim result As WebResponse
Dim proxyObject As New WebProxy("proxy.fake.fake.com:8080", False)
Try

Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader

'Convert the web request to an HttpWebRequest so the UserAgent
can be set
'Don't know if it makes a difference, but the xmlServerHttp
object did this
'so might as well try it here
Dim req As HttpWebRequest = CType(WebRequest.Create(url),
HttpWebRequest)

req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT
5.0)"
req.Proxy = proxyObject

'Tried this but it didn't seem to matter either way
'ensure that it sends default credentials
'req.PreAuthenticate = True

'Dim MyCredential As New System.Net.NetworkCredential("xxxxx",
"xxxxx")

'req.Credentials = MyCredential
'System.Net.CredentialCache.DefaultCredentials

'Dim cache As CredentialCache = New CredentialCache
'cache.Add(New Uri("https://FakeURL/logon.asp"), "Basic", New
NetworkCredential("xxxxx", "xxxxx"))
'req.Credentials = cache

' ----- This block is straight from the .Net samples
----------------------------------------------
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}

If payload <> Nothing Then
Dim i As Integer = 0
Dim j As Integer
While i < payload.Length
j = payload.IndexOfAny(reserved, i)
If j = -1 Then

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(i, payload.Length -
i)))
Exit While
End If

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(i, j - i)))
UrlEncoded.Append(payload.Substring(j, 1))
i = j + 1
End While
SomeBytes =
System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToSt ring())
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()
Else
req.ContentLength = 0
End If

'---------------------------------------------------------------------------------------------------

'Tried this as an alternative forumulation to the block directly
above
'Dim requestBytes() As Byte
'requestBytes = System.Text.Encoding.ASCII.GetBytes(payload)
'RequestStream = req.GetRequestStream()
'RequestStream.Write(requestBytes, 0, requestBytes.Length)

'---------------------------------------------------------------------------------------------------
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream, encode)

Dim read(256) As Char
Dim count As Integer = sr.Read(read, 0, 256)

Dim strbRetval As New System.Text.StringBuilder
Do While count > 0
Dim str As String = New String(read, 0, count)
strbRetval.Append(str)
count = sr.Read(read, 0, 256)
Loop
Return strbRetval.ToString()
Catch Exc As Exception
Return Exc.Message
Finally
If Not result Is Nothing Then
result.Close()
End If
End Try
End Function
End Class
'-------------------------------------------------------------------------------------------------------------------

'ServerXMLHTTP Code
------------------------------------------------------------------------------------

Dim lResolve As Long
Dim lConnect As Long
Dim lSend As Long
Dim lReceive As Long
Dim xmlServerHTTP
xmlServerHTTP = CreateObject("MSXML2.ServerXMLHTTP.4.0")
lResolve = 60000
lConnect = 60000
lSend = 60000
lReceive = 120000

xmlServerHttp.setProxy(2, "proxy.fake.fake.com:8080", "")
xmlServerHttp.open("POST", "https://FakeURL/logon.asp", False)
xmlServerHttp.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE
5.5; Windows NT 5.0)")
xmlServerHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded")
xmlServerHttp.send("Mode=&Username=" & sUID & "&Password=" & sPassword)

While xmlServerHTTP.readyState <> 4
xmlServerHTTP.waitForResponse 1000
Wend
'----------------------------------------------------------------------------------------------------------
--
Dave Brown
Nov 19 '05 #1
1 2317
Dave Brown wrote:
I am attempting to post to a url (https://FakeURL/logon.asp) using
the HttpWebRequest class. The response for a succesful post will
contain the html for the logon user's default page.

We've accomplished this in the past utilizing the ServerXMLHTTP
object. When I try an equivalent? post utilizing the HttpWebRequest
class, the response contains the html for logon.asp (the same page
that was posted to), which indicates to me that in some way my post
didn't work. No exceptions are thrown, it's just that the wrong page
is returned.

The request is through a proxy server to the internet, via https.
I've set the headers the same way as with the successful call through
ServerXMLHTTP. I've tried it both with setting and not setting the
Credentials property of the request. In setting Credentials, I've
tried using a CredentialCache, or just a NetworkCredential

The code for both the ServerXMLHTTP, and the .Net page is below. Can
anyone tell me what I'm missing?


If you're posting to a logon page, and the server issues a session
cookie, your code will lose it. Create a CookieContainer instance and
set the HttpWebRequest.CookieContainer to this instance for all
requests you send.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
16
by: thomas peter | last post by:
I am building a precache engine... one that request over 100 pages on an remote server to cache them remotely... can i use the HttpWebRequest and WebResponse classes for this? or must i use the...
2
by: GlennLanier | last post by:
Hello, I've searched the forums and can't find an answer -- if it i there, kindly point me in that direction. I would like to simulate a browser POSTing a FORM and be able to pars the response....
1
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to...
8
by: Dave Brown | last post by:
I am attempting to post to a url (https://FakeURL/logon.asp) using the HttpWebRequest class. The response for a succesful post will contain the html for the logon user's default page. We've...
2
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
4
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great...
6
by: nganapat | last post by:
I am trying to post form values to a https web page programmatically using Httpwebrequest but no matter what I do the same login page is returned instead of the next page. I would very much...
1
by: sindhurasingeetham | last post by:
Hi, I'm new to coding in .NET. I am trying to do an asynchronous call in my code using httpwebrequest. This code works perfectly fine on one server, but does not work on another. The main flow...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.