Jump to content
Sign in to follow this  
vagabond719

Capturing a web data stream from ticketing system

Recommended Posts

vagabond719

I am hoping someone can help me translate this code into AutoIt. We are using a web based ticketing system at work. Currently I have the queue manager pull the files via the website to plug into my current script. However I found on the Wiki this information to pull reports directly. From what I have been able to find out so far is the provided URL will open a data stream and then the data stream needs to be captured and saved to a file. Let me know if I can provide more detail than what is listed.

From WIKI___________________________________________________________

1. Add the following Imports:

using System.Net;
using System.IO;

2. Call the Download method:

static void Main(string[] args)

{
// Call to DownloadFile method supplying the URL and location to save CSV file locally
int read = DownloadFile("https://myticketsystem.com/incident_list.do?CSV&sysparm_query=priority=1&sysparm_orderby=assigned_to",
"c:\\test\\incident.csv");
}

3. Create a Download method as follows:

public static int DownloadFile(String url,
String localFilename)
{
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;
// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;
// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(url);
// Create the credentials required for Basic Authentication
System.Net.ICredentials cred = new System.Net.NetworkCredential("user_name", "password");
// Add the credentials to the request
request.Credentials = cred;
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object
response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
// Create the local file
localStream = File.Create(localFilename);
// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Close the response and streams objects here
// to make sure they're closed even if an exception
// is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
// Return total bytes processed to caller.
return bytesProcessed;
}

Share this post


Link to post
Share on other sites
FireFox

Hi,

Here you go (not tested) :

;~ using System.Net;
;~ using System.IO;
#include <WinHTTP.au3>
#include <FileConstants.au3>

Main()
;~ static void Main(string[] args)
;~ {
Func Main()
    ; Call to DownloadFile method supplying the URL and location to save CSV file locally
    Local $hRead = InetGet("https://myticketsystem.com/incident_list.do?CSV&sysparm_query=priority=1&sysparm_orderby=assigned_to", _
        "c:\test\incident.csv")
    InetClose($hRead)
;~ }
EndFunc

;~ public static int DownloadFile(String url,
;~ String localFilename)
;~ {
Func DownloadFile($sURL, $sLocalFilename)
    ; Function will return the number of bytes processed
    ; to the caller. Initialize to 0 here.
;~  int bytesProcessed = 0;
    Local $iBytesProcessed = 0
    ; Assign values to these objects here so that they can
    ; be referenced in the finally block
;~  Stream remoteStream = null;
;~  Stream localStream = null;
;~  WebResponse response = null
    Local $bRemoteStream = Binary(""), $hLocalStream = 0, $bResptmp = $bRemoteStream
    ; Use a try/catch/finally block as both the WebRequest and Stream
    ; classes throw exceptions upon error
;~  try
;~  {
    ; Create a request for the specified remote file name
;~  WebRequest request = WebRequest.Create(url);

    Local $hWinHttpOpen = 0, $hWinHttpConnect = 0, $hWinHttpRequest = 0

    $hWinHttpOpen = _WinHttpOpen()
    If @error Then Return 0

    Local $aHost = StringRegExp($sURL, "https?://(.*?)/", 3)
    $hWinHttpConnect = _WinHttpConnect($hWinHttpOpen, $aHost[0])
    If @error Then Return 0

    ; Create the credentials required for Basic Authentication
;~  System.Net.ICredentials cred = new System.Net.NetworkCredential("user_name", "password");

    ; Add the credentials to the request
;~  request.Credentials = cred;
    _WinHttpSetCredentials($hWinHttpConnect, $WINHTTP_AUTH_TARGET_SERVER, $WINHTTP_AUTH_SCHEME_BASIC, "user_name", "password")
    If @error Then Return 0

    ; Send the request to the server and retrieve the
    ; WebResponse object
;~  response = request.GetResponse();
    $hWinHttpRequest = _WinHttpOpenRequest($hWinHttpConnect, "GET", _
            StringTrimLeft($sURL, StringInStr($sURL, "/", 2, 3) -1))
;~  if (request != null)
;~  {
    If @error Then Return 0

    _WinHttpSendRequest($hWinHttpRequest)
    If @error Then Return 0

    _WinHttpReceiveResponse($hWinHttpRequest)
;~  if (response != null)
;~  {
    If @error Then Return 0

    ; Once the WebResponse object has been retrieved,
    ; get the stream object associated with the response's data
;~  remoteStream = response.GetResponseStream();

    ; Create the local file
;~  localStream = File.Create(localFilename);
    $hLocalStream = FileOpen($sLocalFilename, BitOR($FO_BINARY, $FO_OVERWRITE))

    ; Allocate a 1k buffer
;~  byte[] buffer = new byte[1024];
;~  int bytesRead;
    Local $iBytesRead = 0
    ; Simple do/while loop to read from stream until
    ; no bytes are returned

;~  do
;~  {
    While 1
        ; Read data (up to 1k) from the stream
;~      bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
        $bResptmp = _WinHttpReadData($hWinHttpRequest, 2) ;binary
        If @error Then ExitLoop

        ; Increment total bytes processed
;~      bytesProcessed += bytesRead;
        $iBytesProcessed += @extended

        ; Write the data to the local file
;~      localStream.Write(buffer, 0, bytesRead);
        FileWrite($hLocalStream, $bResptmp)
    WEnd
;~  } while (bytesRead > 0);
;~  }
;~  }
;~  }
;~  catch (Exception e)
;~  {
;~      Console.WriteLine(e.Message);
;~  }
;~  finally
;~  {
    ; Close the response and streams objects here
    ; to make sure they're closed even if an exception
    ; is thrown at some point
;~  if (response != null) response.Close();
;~  if (remoteStream != null) remoteStream.Close();
    _WinHttpCloseHandle($hWinHttpRequest)
    _WinHttpCloseHandle($hWinHttpConnect)
    _WinHttpCloseHandle($hWinHttpOpen)
;~  if (localStream != null) localStream.Close();
    FileClose($hLocalStream)
;~  }
    ; Return total bytes processed to caller.
;~  return bytesProcessed;
    Return $iBytesProcessed
    ;~ }
EndFunc

Note: I doubt that the InetGet function will work with your url, you will have to use the WinHTTP UDF like I did for the DownloadFile function.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×