Results 1 to 8 of 8

Thread: [RESOLVED] HTTP query

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [RESOLVED] HTTP query

    Hi,

    I have a http command to query the number of SMS left in my account. When i hit the following address in the address bar i get the result as 'Your balance is 642' in the web page.

    http://www.mlmsms.in/balancecheck.ph...=your_password

    I am using the following code to extract this information and use it in my project but somehow cannot figure out why i am not getting the result in 'Label19'. Please Help!

    Code:
    Dim url As New System.Uri("http://www.mlmsms.in/balancecheck.php?username=your_username&password=your_password")
                'Request for request
                Dim req As System.Net.WebRequest
                req = System.Net.WebRequest.Create(url)
                Dim resp As System.Net.WebResponse
                Try
                    resp = req.GetResponse()
                    Me.Label19.Text = resp.ToString
                    resp.Close()
    
                    req = Nothing
                Catch ex As Exception
                    req = Nothing
                End Try

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: HTTP query

    Hey,

    The website that you are using puts your username and password in the QueryString?!?! I find that hard to believe?!? This would be a very insecure way to do things!!

    Have you stepped through your code in the debugger? Are there any exceptions? If so, what are they?

    Gary

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: HTTP query

    Hi,

    Yes thats how the SMS service provider has given the syntax to query the remaining balance using http. All i want is to extract the result and convert it to string. There is no exception however i get " System.Net.HttpWebResponse" in label19.text.

    How can i get the result in label19.text as the one in webpage?

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: HTTP query

    Hey,

    This is because ToString is returning the Type of the class you are using, not the contents of it. You are going to need to read the Stream information that has come down with the response.

    Have a look at the link in my signature, Compressed HttpWebRequest, it is specifically for using a compressed request/response, but the code is the similar for what you are trying to do.

    Hope that helps!

    Gary

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [RESOLVED] HTTP query

    Thanks GEP,

    It works well but it seems to take lot of time even on my broadband connection.. is there a faster way to do this??

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] HTTP query

    Hey,

    Can you show the final code that you are using? Also, how long are we talking about here?

    Does it take a long time if you browse to the url directly in your browser?

    Gary

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [RESOLVED] HTTP query

    i am using the following:

    vb Code:
    1. Public Function GetUrl(ByVal Url As String, ByVal PostData As String, ByVal GZip As Boolean) As String
    2.         Dim Http As HttpWebRequest = WebRequest.Create(Url)
    3.         If GZip = True Then
    4.             Http.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
    5.         End If
    6.  
    7.         If Not String.IsNullOrEmpty(PostData) Then
    8.             Http.Method = "POST"
    9.             Dim lbPostBuffer As Byte() = Encoding.Default.GetBytes(PostData)
    10.  
    11.             Http.ContentLength = lbPostBuffer.Length
    12.  
    13.             Using PostStream As Stream = Http.GetRequestStream()
    14.                 PostStream.Write(lbPostBuffer, 0, lbPostBuffer.Length)
    15.             End Using
    16.         End If
    17.  
    18.         Using WebResponse As HttpWebResponse = Http.GetResponse()
    19.  
    20.             Dim responseStream As Stream = WebResponse.GetResponseStream()
    21.  
    22.             If (WebResponse.ContentEncoding.ToLower().Contains("gzip")) Then
    23.                 responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
    24.             ElseIf (WebResponse.ContentEncoding.ToLower().Contains("deflate")) Then
    25.                 responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
    26.             End If
    27.  
    28.             Dim reader As StreamReader = New StreamReader(responseStream, Encoding.Default)
    29.  
    30.             Dim html As String = reader.ReadToEnd()
    31.  
    32.             responseStream.Close()
    33.             Return html
    34.         End Using
    35.  
    36.     End Function

    And for calling the result:

    vb Code:
    1. Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    2.         Me.Button7.Text = GetUrl("http://www.mlmsms.in/balancecheck.php?username=username&password=password", String.Empty, True)
    3.     End Sub

    Something needs to be changed?

    No, it doesn't take more than a second or two when using browser.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] HTTP query

    Hey,

    Ok, so assuming the website isn't using compressed headers, try the following:

    Code:
    Me.Button7.Text = GetUrl("http://www.mlmsms.in/balancecheck.php?username=username&password=password", String.Empty, False)
    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width