Solved

how HTTP request is constructed and how to use System.Net.HttpWebRequest???

Posted on 2009-04-02
12
772 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:CalmSoul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24049317
This code does a simple GET to www.google.com
' Create the HttpWebRequest to www.google.com
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.google.com"), HttpWebRequest)
 
Dim resp As HttpWebResponse = Nothing
Dim respStream As Stream = Nothing
Dim responseText As String = Nothing
Try
    ' Get the HttpWebResponse, and it's response stream
    resp = DirectCast(req.GetResponse(), HttpWebResponse)
    respStream = resp.GetResponseStream()
    
    ' Read the response text into a string
    Dim reader As New StreamReader(respStream)
    responseText = reader.ReadToEnd()
    
Finally
    ' Always, always, always close the response stream
    ' If you don't, you might run out of connections
    
    If respStream IsNot Nothing Then
        respStream.Close()
    End If
End Try

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24049332
what imports I need for this?

system.net and ??
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24049374
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
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 5

Author Comment

by:CalmSoul
ID: 24049386
How I password credentials to it?

user name and password ?
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24049421
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?
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24049469
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

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24049515
I read couple of articles that System.Net.HttpWebRequest outputs the XML... then we can do something like this to authenticate ...

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

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24049789
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?
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24051117
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
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

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24052459
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

Open in new window

0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24052663
The code that goes after ' And the get the response,is the all but the first line of the code sample I provided in my first post.

So you've got an example of how to request a page using GET (default method) and POST. It looks like you'll have to POST to the login page 1st, keeping track of the cookies returned by the server.
Then create a new request to get the file, using the same cookie container used in the login request.

Dim cookies As New CookieContainer();
 
' Login request
Dim loginReq As HttpWebRequest ' as per example
loginReq.CookieContainer = cookies
 
' more code for handling response as per example
 
' Get file request (or get xml request)
Dim fileReq As HttpWebRequest
fileReq .CookieContainer = cookies
 
 
' more code for handling response as per example

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24052966
Getting following error
at System.Net.HttpWebRequest.GetRequestStream() at _Default.Button1_Click(Object sender, EventArgs e) in

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question