Http login through code
Http login through code
Http login through code
Http login through code
Http login through code
Http login through code Http login through code Http login through code Http login through code Http login through code Http login through code Http login through code Http login through code
Http login through code Http login through code
Http login through code
Go Back  Xtreme Visual Basic Talk > > > Http login through code


Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2014, 02:19 AM
JustinCase2 JustinCase2 is offline
Junior Contributor
 
Join Date: Apr 2006
Posts: 324
Default Http login through code


I am having some trouble login in to a webpage through code. I have an URL that should Return json data. The URL is behind a login. Here is the example I have on how to accomplish this through jQuery:

Following codes show how to signin using jQuery:

Code:
$.ajax({url: "http://HOSTIP/signin.php",
type: 'POST', dataType: 'json',
data: {'user[name]': 'admin', 'user[password]': 'mypwd'}
}).done(function(data) {
console.debug("signin done");
});
Then how to get my json data:

Code:
var url = 'http://HOSTIP/data_api.php?url=/folder/Value.out';
$.ajax({
url: url, type: 'GET', dataType: 'json'
}).done(function(data) {
console.debug(data);
});
I can't seem to translate this into VB code. Here is what I have so far:

Code:
        Dim request As HttpWebRequest = HttpWebRequest.Create("http://HOSTIP/signin.php")
        request.Credentials = New NetworkCredential("admin", "mypwd")
        request.Method = WebRequestMethods.Http.Post
        request.AllowAutoRedirect = True
        Dim response As HttpWebResponse = request.GetResponse()
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        WriteToTextFile("c:\Test.txt", responseFromServer)
But the response I get is just the full HTML for the login page. Not sure where to go from here. Anyone?
Reply With Quote
  #2  
Old 10-02-2014, 04:43 AM
PlausiblyDamp's Avatar
PlausiblyDampHttp login through code PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

It looks like the login is simply expecting a Json object with two properties - the username and password rather than a full blown set of credentials - try posting the json directly and see if that helps.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 10-02-2014, 05:48 AM
JustinCase2 JustinCase2 is offline
Junior Contributor
 
Join Date: Apr 2006
Posts: 324
Default Thanks for your post

I have attempted this, With the following code:

Code:
Dim jsonString As String = "{'user[name]':'admin','user[password]':'mypwd'}"
        Dim data = Encoding.UTF8.GetBytes(jsonString)
        Dim u As New Uri("http://HOSTIP/signin.php")
        Dim result_post = SendRequest(u, data, "application/json", "POST")
        WriteToTextFile("C:\test.txt", result_post)
SendRequest:

Code:
Private Shared Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
        Dim req As WebRequest = WebRequest.Create(uri)
        req.ContentType = contentType
        req.Method = method
        req.ContentLength = jsonDataBytes.Length
        Dim stream = req.GetRequestStream()
        stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
        stream.Close()
        Dim response = req.GetResponse().GetResponseStream()
        Dim reader As New StreamReader(response)
        Dim res = reader.ReadToEnd()
        reader.Close()
        response.Close()
        Return res
    End Function
Still the result i get is the full HTML of the login page.
Am I approaching this wrong? I don't really need a result string from the login request, i just need to log in to get to the data page.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Http login through code
Http login through code
Http login through code Http login through code
Http login through code
Http login through code
Http login through code Http login through code Http login through code Http login through code Http login through code Http login through code Http login through code
Http login through code
Http login through code
 
Http login through code
Http login through code
 
-->