CalmSoul
asked on
how HTTP request is constructed and how to use System.Net.HttpWebRequest???
Hello Experts:
Can somebody provide me code snippet of how HTTP request is constructed and how to use System.Net.HttpWebRequest? ??
I am using VB.NET in ASP.NET web application
Regards,
CS
Can somebody provide me code snippet of how HTTP request is constructed and how to use System.Net.HttpWebRequest?
I am using VB.NET in ASP.NET web application
Regards,
CS
ASKER
what imports I need for this?
system.net and ??
system.net and ??
and System.IO
just a tip, if you're using VS, hold the mouse over the red squigly line, you should see a red rectangle under the last character, hold your mouse over that and it'll tell you what namespace you can import.
imports.png
just a tip, if you're using VS, hold the mouse over the red squigly line, you should see a red rectangle under the last character, hold your mouse over that and it'll tell you what namespace you can import.
imports.png
ASKER
How I password credentials to it?
user name and password ?
user name and password ?
ASKER
I copy pasted this code on the button click ... and when I click on the button its talking me to localhost... and page just post back and google never shows up?
You'd have to inspect the form you're posting to, to determine what fields you need. Then create the post data:
' Create the HttpWebRequest to www.google.com
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.google.com"), HttpWebRequest)
req.Method = "POST"
' Form post data
Dim postData As String = String.Format("username={0}&password={1}", "myusername", "mypassword")
Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(postData)
' Add the post data to the request
req.ContentLength = buffer.Length
Dim reqStream As Stream = req.GetRequestStream()
reqStream.Write(buffer, 0, buffer.Length)
reqStream.Close()
' Add a CookieContainer to the request to we can keep track of a session
req.CookieContainer = new CookieContainer();
' And the get the response
ASKER
I read couple of articles that System.Net.HttpWebRequest outputs the XML... then we can do something like this to authenticate ...
Is this true?
Is this true?
strStubUserName = UserName
strStubPassword = Password
'Set up user and password parameters
Set objXMLBody = CreateObject("MSXML.DOMDocument")
REQUEST_TEXT = "<?xml version='1.0' ?><authorization><username>" & strStubUserName & "</username><password>" & strStubPassword & "</password></authorization>"
blnXMLLoad = objXMLBody.loadXML(REQUEST_TEXT)
If blnXMLLoad = False Then
Call modMain.WriteErrorLog
End If
I'm not entirely sure what you're asking.
I get the vague idea that you're confusing the System.Net.HttpWebRequest & System.Net.HttpWebResponse with System.Web.HttpWebRequest & System.Web.HttpWebResponse
See:
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx#ctl00_rs1_mainContentContainer_cpe315316_c
As for the Xml, are you trying to generate it or consume it, are you trying to authenticate on a 3rd party's site, or are you trying to add authentication to your own site?
I get the vague idea that you're confusing the System.Net.HttpWebRequest & System.Net.HttpWebResponse
See:
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx#ctl00_rs1_mainContentContainer_cpe315316_c
As for the Xml, are you trying to generate it or consume it, are you trying to authenticate on a 3rd party's site, or are you trying to add authentication to your own site?
ASKER
I am trying to get document download from server.
Here is the old vb code ... written using MSXML... now they recommend to use System.Net.HttpWebRequest
Here is the old vb code ... written using MSXML... now they recommend to use System.Net.HttpWebRequest
Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public URL As String
Public UserName As String
Public Password As String
Public HOST As String
Function SaveAsCommonDialog(Optional sTitle = "Save File", Optional sFilter As String, Optional sDefaultDir As String) As String
Const clBufferLen As Long = 255
Dim OFName As OPENFILENAME, sBuffer As String * clBufferLen
On Error GoTo ExitFunction
OFName.lStructSize = Len(OFName)
OFName.hwndOwner = GetActiveWindow 'or Me.hwnd in VB
OFName.hInstance = 0 'or App.hInstance in VB
If Len(sFilter) Then
OFName.lpstrFilter = sFilter
Else
OFName.lpstrFilter = "Text Files (*.txt)" & Chr$(0) & "*.txt" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" & Chr$(0)
End If
OFName.lpstrFile = sBuffer
OFName.nMaxFile = clBufferLen 'Set max number of characters
OFName.lpstrFileTitle = sBuffer
OFName.nMaxFileTitle = clBufferLen 'Set max number of characters
'Set the initial directory
If Len(sDefaultDir) Then
OFName.lpstrInitialDir = sDefaultDir
Else
OFName.lpstrInitialDir = CurDir$
End If
OFName.lpstrTitle = sTitle
OFName.flags = 0
'Show dialog
If GetSaveFileNameA(OFName) Then
SaveAsCommonDialog = Left$(OFName.lpstrFile, InStr(1, OFName.lpstrFile, Chr(0)) - 1)
Else
SaveAsCommonDialog = ""
End If
ExitFunction:
On Error GoTo 0
End Function
' ===========================================================================
' LogOn
'
' Purpose:
' Logon using WEBDAV
'
' Output:
' HTTP Response Header
' ===========================================================================
Public Function Logon(ByVal strUserID As String) As String
Dim request As New MSXML.XMLHTTPRequest
Dim objXMLBody As Object
Dim REQUEST_TEXT As String
Dim RESOURCE_URL As String
Dim strStubUserName As String
Dim strStubPassword As String
Dim blnXMLLoad As Boolean
Dim lngLoginLoop As Long
strStubUserName = UserName
strStubPassword = Password
'Set up user and password parameters
Set objXMLBody = CreateObject("MSXML.DOMDocument")
REQUEST_TEXT = "<?xml version='1.0' ?><authorization><username>" & strStubUserName & "</username><password>" & strStubPassword & "</password></authorization>"
blnXMLLoad = objXMLBody.loadXML(REQUEST_TEXT)
If blnXMLLoad = False Then
Call modMain.WriteErrorLog
End If
RESOURCE_URL = "dscgi/ds.py/Login"
'Post request
request.abort
request.Open "POST", "HTTP://" & URL & "/" + RESOURCE_URL, False
request.setRequestHeader "Host", HOST
request.setRequestHeader "USer-Agent", "DsAxess/3.1"
request.setRequestHeader "Content-Type", "text/xml"
request.setRequestHeader "Content-Length", Len(REQUEST_TEXT)
request.setRequestHeader "Accept", "text/xml"
request.send objXMLBody
'Check the outcome of the request and return true for success or false for failed
If request.Status = "200" Then
'Login Succeeded
Logon = request.getResponseHeader("Set-Cookie")
Else
'Login failed
Logon = request.getResponseHeader("Set-Cookie")
End If
'Clean up
request.abort
Set objXMLBody = Nothing
Set request = Nothing
End Function
' ===========================================================================
' GetXML
'
' Purpose:
' This function returns XML for any DocuShare Operation
'
' Output:
' HTTP Response in XML format
' ===========================================================================
Public Function GetXML(ByVal strParentHandle As String, ByVal REQUEST_TEXT As String, ByVal Operation As String) As String
'On Error GoTo ErrorHandler
Dim request As MSXML.XMLHTTPRequest
Dim objXMLBody As Object
Dim RESOURCE_URL As String
Dim blnXMLLoad As Boolean
Dim strAuthToken As String
'Make request url
RESOURCE_URL = "dscgi/ds.py/" & Operation & "/" & strParentHandle
'Logon
strAuthToken = Logon(UserName)
'Set up search
Set request = New MSXML.XMLHTTPRequest
request.abort
request.Open "POST", "HTTP://" & URL & "/" & RESOURCE_URL, False
request.setRequestHeader "Host", HOST
request.setRequestHeader "User-Agent", "DsAxess/3.1"
request.setRequestHeader "Content-Type", "text/xml"
request.setRequestHeader "Content-Length", Len(REQUEST_TEXT)
request.setRequestHeader "Set-Cookie", strAuthToken
request.setRequestHeader "Accept", "text/xml"
'Get firm collection handle
Set objXMLBody = CreateObject("MSXML.DOMDocument")
blnXMLLoad = objXMLBody.loadXML(REQUEST_TEXT)
If blnXMLLoad = False Then
Call modMain.WriteErrorLog
End If
'Send Request
request.send objXMLBody
'Return result
GetXML = request.responseText 'request.getAllResponseHeaders
'Debug.Print request.responseText
Debug.Print "Status " & request.Status & ": " & request.statusText
'Clean up
request.abort
Set request = Nothing
Set objXMLBody = Nothing
End Function
Function ModifyACL(ByVal CurrentXML As MSXML.DOMDocument) As String
On Error GoTo EH
Dim nlTemp As MSXML.IXMLDOMNodeList
Dim root As MSXML.IXMLDOMElement
Dim CurrentNode As MSXML.IXMLDOMNode
Dim oldChildNode As MSXML.IXMLDOMNode
Dim bResult As Boolean
Set nlTemp = CurrentXML.getElementsByTagName("parents")
CurrentXML.Save (App.Path & "\temp.xml")
Set root = nlTemp.Item(0)
Debug.Print root.xml
Debug.Print nlTemp.length
bResult = CurrentXML.loadXML("<dsref handle=""Collection-11""><displayname>Initial Top-Level Collection B</displayname></dsref>")
Set CurrentNode = CurrentXML.firstChild
Debug.Print CurrentNode.xml
Set CurrentNode = root.appendChild(CurrentNode)
Debug.Print root.xml
ModifyACL = root.xml
Set nlTemp = Nothing
Set root = Nothing
Set CurrentNode = Nothing
Set oldChildNode = Nothing
Exit Function
EH:
Err.Raise Err.Number
ModifyACL = ""
End Function
' ===========================================================================
' GetFile
'
' Purpose:
' This function downloads file from Docushare server
'
' Output:
' HTTP Response in XML format
' ===========================================================================
Public Function GetFile(ByVal strHandle As String, ByVal strVersion As String) As String
'On Error GoTo ErrorHandler
Dim request As New MSXML.XMLHTTPRequest
Dim stream As New ADODB.stream
Dim RESOURCE_URL As String
Dim blnXMLLoad As Boolean
Dim strAuthToken As String
Dim sFilePath As String
'Make request url
' RESOURCE_URL = "dsweb/Get/" & strHandle ' gets preferred version
RESOURCE_URL = "dsweb/GetVersion/" & strHandle & "/" & strVersion
'Logon
strAuthToken = Logon(UserName)
'Set up search
request.abort
request.Open "GET", "HTTP://" & URL & "/" & RESOURCE_URL, False
request.setRequestHeader "Host", HOST
request.setRequestHeader "User-Agent", "DsAxess/3.1"
request.setRequestHeader "Set-Cookie", strAuthToken
request.setRequestHeader "Accept", "text/xml"
'Send Request
request.send
If stream.State = adStateClosed Then
stream.Open
End If
stream.Type = adTypeBinary
stream.Write request.responseBody
sFilePath = SaveAsCommonDialog("Save File As", "*.*", App.Path)
stream.SaveToFile sFilePath, adSaveCreateOverWrite
stream.Close
'Return result
GetFile = request.responseText
Debug.Print request.getAllResponseHeaders
Debug.Print "Status " & request.Status & ": " & request.statusText
'Clean up
Set stream = Nothing
request.abort
Set request = Nothing
End Function
ASKER
Is this .. How do I get a response?
' Create the HttpWebRequest to www.google.com
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.google.com"), HttpWebRequest)
req.Method = "POST"
' Form post data
Dim postData As String = String.Format("username={0}&password={1}", "myusername", "mypassword")
Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(postData)
' Add the post data to the request
req.ContentLength = buffer.Length
Dim reqStream As Stream = req.GetRequestStream()
reqStream.Write(buffer, 0, buffer.Length)
reqStream.Close()
' Add a CookieContainer to the request to we can keep track of a session
req.CookieContainer = new CookieContainer();
' And the get the response
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Getting following error
at System.Net.HttpWebRequest.GetRequestStream() at _Default.Button1_Click(Object sender, EventArgs e) in
Open in new window