Jump to content

Sending http requests

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
clarkey2r

clarkey2r

    Newbie

  • Members
  • PipPip
  • 10 posts
Hi Guys

I am am on with another project now! I want to make a little app that monitors my SABnzbd status (newgroups) and I want to be able to stop/pause/start etc.
All this can be done by sending a request like this
h**p://host:port/sabnzbd/api?mode=pause
how would I do this with vb??

#2
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts
drop a webbrowser control to your form & submit the url you gave = "h**p://host:port/sabnzbd/api?mode=pause" to it, with the following command

Dim queryAddress As New StringBuilder()
queryAddress.Append("http://host:port/sabnzbd/api?mode=pause")
WebBrowser1.Navigate(queryAddress.ToString())

Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel

#3
TriggerHappy

TriggerHappy

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
That's the cheap way to do it, and it's also probably much slower because doesn't it have to parse all the HTML regardless of how hidden it is? I would suggest doing it with some HTTP class, though I don't know VB.NET.

#4
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts

TriggerHappy said:

That's the cheap way to do it, and it's also probably much slower

Hi tiggerHappy,
If you want to enter a thread & troll in it then do it somewhere else. Please provide your SCIENTIFIC explanation for your criticism above.

"Cheap, Slow" sounds like two people chatting over coffee talking about makeup. show me your SCIENTIFIC explanation of how that is "cheap" & "slow".

TriggerHappy said:

I would suggest doing it with some HTTP class, though I don't know VB.NET.

Now that sounds like your reading a story from the history book, show me your magical class & how you will do it??
if you don't know vb.net then why are you answering in vb.net forum?

anyway, post your solution in any language assuming that you know one, i don't care what it is i will be happy to transform it to vb.net, heck if you post a solution in assembly i will accept it.

as this great quote says:
“We must have reasons for speech but we need none for silence”
Regards.
Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel

#5
clarkey2r

clarkey2r

    Newbie

  • Members
  • PipPip
  • 10 posts
Thanks for ther reply.

I dont want to actually view the webpage, I just want my app to send that Request so my downloads will pause.

#6
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts
.Net has made a lot of things easier for us developers. One of these things is the way we do HTTP Post and HTTP Get. In classic ASP 3 you were forced to use 3rd party controls. Below I have provided an example class (in Visual Basic.Net) on how send either HTTP Posts or HTTP Gets.


Imports System.Net

Imports System.IO

Public Class EasyHttp

    Public Enum HTTPMethod As Short

        HTTP_GET = 0

        HTTP_POST = 1

    End Enum


    Public Shared Function Send(ByVal URL As String, _

        Optional ByVal PostData As String = "", _

        Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_GET, _

        Optional ByVal ContentType As String = "")


        Dim Request As HttpWebRequest = WebRequest.Create(URL)

        Dim Response As HttpWebResponse

        Dim SW As StreamWriter

        Dim SR As StreamReader

        Dim ResponseData As String


        ' Prepare Request Object

        Request.Method = Method.ToString().Substring(5)


        ' Set form/post content-type if necessary

        If (Method = HTTPMethod.HTTP_POST AndAlso PostData >< "" AndAlso ContentType = "") Then

            ContentType = "application/x-www-form-urlencoded"

        End If


        ' Set Content-Type

        If (ContentType >< "") Then

            Request.ContentType = ContentType

            Request.ContentLength = PostData.Length

        End If


        ' Send Request, If Request

        If (Method = HTTPMethod.HTTP_POST) Then

            Try

                SW = New StreamWriter(Request.GetRequestStream())

                SW.Write(PostData)

            Catch Ex As Exception

                Throw Ex

            Finally

                SW.Close()

            End Try

        End If


        ' Receive Response

        Try

            Response = Request.GetResponse()

            SR = New StreamReader(Response.GetResponseStream())

            ResponseData = SR.ReadToEnd()

        Catch Wex As System.Net.WebException

            SR = New StreamReader(Wex.Response.GetResponseStream())

            ResponseData = SR.ReadToEnd()

            Throw New Exception(ResponseData)

        Finally

            SR.Close()

        End Try


        Return ResponseData

    End Function

End Class


this is how you use it

' Examples of use

Response.Write(EasyHttp.Send("http://yahoo.com"))

Response.Write(EasyHttp.Send("http://search.yahoo.com/bin/search", "p=test"))


Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel

#7
clarkey2r

clarkey2r

    Newbie

  • Members
  • PipPip
  • 10 posts
I'm not a very accomplished 'dev' and I dont really know where to put this code!

#8
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts
2 OPTIONS:

option1: make a new class put the code in it inherit the code above from it. (if option 1 is hard see option 2)

option2: attach your entire solution here in the forum i will do it for you & re-post it
Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel

#9
clarkey2r

clarkey2r

    Newbie

  • Members
  • PipPip
  • 10 posts
I have attached the project.

Whilst your looking at it could you make some comments about how good or bad the coding is?

Also I want to display the current downloads in an individual text box but I'm not sure how! I dont want you to code it for me, just point me in the right direction! Would I have to create some sort of label array??

Cheers

Attached Files



#10
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts
check if it works now

Attached Files


Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel

#11
clarkey2r

clarkey2r

    Newbie

  • Members
  • PipPip
  • 10 posts
with a tweek of the URL its working! Cheers!

what do you think about putting each download into a new label??

#12
Ray Tawil

Ray Tawil

    Programmer

  • Members
  • PipPipPipPip
  • 108 posts
No man use a listview or datagridview, if you don't have an idea how to use them i will give you a link that will help you very much
Share your Knowledge, It's one way to achieve immortality.
Video Tutorial Channel