![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
In .NET is very simple: Only 1 line of code: = )System.Net.WebRequest request = System.Net.WebRequest.Create(https://mvp.support.microsoft.com); The System.Net.WebRequest supports "http:", "https:" and "file:".. Here is a complete example on how to open a https connection in J#.NET and print the information on the secure page in a command prompt. public class Class1 { public Class1() { // Open a https connection System.Net.WebRequest request = System.Net.WebRequest.Create("https://mvp.support.microsoft.com"); // Get the response stream System.Net.WebResponse TheResponse = request.GetResponse(); // Write the headers System.Console.WriteLine( TheResponse.get_Headers().toString() ); // Get the connection stream System.IO.Stream stream = TheResponse.GetResponseStream(); // Using a stream reader (to read a complete line etc) System.IO.StreamReader sReader = new System.IO.StreamReader( stream ); // Outputs all lines from the stream String line = ""; while ( (line = sReader.ReadLine()) != null ) { System.Console.WriteLine( line ); } // Close the connection TheResponse.Close(); } /** @attribute System.STAThread() */ public static void main(String[] args) { new Class1(); } } Regards, Lars-Inge Tønnessen |
#4
| |||
| |||
|
|
Hi Tønnessen , Hello, )TheResponse.get_Headers().toString() as i am unnable to find any property by the name of get_headers and it's giving me error even on design time. Maybe your using C# or VB.NET? Please try Headers().toString(). "get_" and "set_" is how J# is accessing .NET properites. System.Console.WriteLine( TheResponse.Headers().toString() ); You can drop it you you want. It only prints out the http headers. Also do we need to pass any special parameters for HTTPS site or it is samefro both HTTP or HTTPS site. Writing "https://" should do the trick for a secure connection. I am having valid username and password and trying to post the form. This sample code works for my web server. Please check the post id name in the html source. I think its Username and Password on your site. Please remeber to use a "&" between "Username=..." and "Password=...". Post it as UTF8 and ubytes to the web server. package httpconpost; public class Class1 { public Class1() { // Open a https connection System.Net.WebRequest request = System.Net.WebRequest.Create("https://corpmis.ggn.hcltech.com/"); // Post login request.set_Method( "POST" ); request.set_ContentType( "application/x-www-form-urlencoded" ); System.IO.Stream streamOut = request.GetRequestStream(); String postMess = "Username=YourLoginName&Password=YourPassword" ; ubyte byt[] = System.Text.Encoding.get_UTF8().GetBytes( postMess ); streamOut.Write( byt, 0, byt.length ); streamOut.Close(); // Get the response stream System.Net.WebResponse TheResponse = request.GetResponse(); // Write the headers System.Console.WriteLine( TheResponse.get_Headers().toString() ); // Get the connection stream System.IO.Stream stream = TheResponse.GetResponseStream(); // Using a stream reader (to read a complete line etc) System.IO.StreamReader sReader = new System.IO.StreamReader( stream ); // Outputs all lines from the stream String line = ""; while ( (line = sReader.ReadLine()) != null ) { System.Console.WriteLine( line ); } // Close the connection TheResponse.Close(); } /** @attribute System.STAThread() */ public static void main(String[] args) { new Class1(); } } Please HELP........... Did this help ? Best Regards, Lars-Inge Tønnessen |
#5
| |||
| |||
|
|
Hi Tønnessen, Thanks for the Reply! I have converted the code given by you in VB.NET and it is given below for your reference. . I am getting the error at the following line Dim streamOut As System.IO.Stream = request.GetRequestStream() also i am not getting the Ubyte datatype in VB.NET. am i missing something? Please help!!!! /*Code Starts */ Dim request As System.Net.WebRequest = System.Net.WebRequest.Create("https://corpmis.ggn.hcltech.com/") ' Post login request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" Dim streamOut As System.IO.Stream = request.GetRequestStream() Dim postMess As String = "Username=username&Password=#pass#123456789" Dim byt As Byte() = System.Text.Encoding.UTF8().GetBytes(postMess) streamOut.Write(byt, 0, byt.Length) streamOut.Close() ' Get the response stream Dim TheResponse As System.Net.WebResponse = request.GetResponse() ' Write the headers System.Console.WriteLine(TheResponse.Headers().ToS tring()) ' Get the connection stream Dim stream As System.IO.Stream = TheResponse.GetResponseStream() ' Using a stream reader (to read a complete line etc) Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(stream) 'Outputs all lines from the stream Dim line As String = "" 'While ((line = sReader.ReadLine()) <> null) ' System.Console.WriteLine(line) 'End While ' Close the connection TheResponse.Close() /* Code Ends */ "Lars-Inge Tønnessen [VJ# MVP]" wrote: Hi Tønnessen , Hello, )TheResponse.get_Headers().toString() as i am unnable to find any property by the name of get_headers and it's giving me error even on design time. Maybe your using C# or VB.NET? Please try Headers().toString(). "get_" and "set_" is how J# is accessing .NET properites. System.Console.WriteLine( TheResponse.Headers().toString() ); You can drop it you you want. It only prints out the http headers. Also do we need to pass any special parameters for HTTPS site or it is samefro both HTTP or HTTPS site. Writing "https://" should do the trick for a secure connection. I am having valid username and password and trying to post the form. This sample code works for my web server. Please check the post id name in the html source. I think its Username and Password on your site. Please remeber to use a "&" between "Username=..." and "Password=...". Post it as UTF8 and ubytes to the web server. package httpconpost; public class Class1 { public Class1() { // Open a https connection System.Net.WebRequest request = System.Net.WebRequest.Create("https://corpmis.ggn.hcltech.com/"); // Post login request.set_Method( "POST" ); request.set_ContentType( "application/x-www-form-urlencoded" ); System.IO.Stream streamOut = request.GetRequestStream(); String postMess = "Username=YourLoginName&Password=YourPassword" ; ubyte byt[] = System.Text.Encoding.get_UTF8().GetBytes( postMess ); streamOut.Write( byt, 0, byt.length ); streamOut.Close(); // Get the response stream System.Net.WebResponse TheResponse = request.GetResponse(); // Write the headers System.Console.WriteLine( TheResponse.get_Headers().toString() ); // Get the connection stream System.IO.Stream stream = TheResponse.GetResponseStream(); // Using a stream reader (to read a complete line etc) System.IO.StreamReader sReader = new System.IO.StreamReader( stream ); // Outputs all lines from the stream String line = ""; while ( (line = sReader.ReadLine()) != null ) { System.Console.WriteLine( line ); } // Close the connection TheResponse.Close(); } /** @attribute System.STAThread() */ public static void main(String[] args) { new Class1(); } } Please HELP........... Did this help ? Best Regards, Lars-Inge Tønnessen |
#6
| |||
| |||
|
|
I'm not a VB.NET expert, I'm only writing VB.NET code when I'm forced to by my boss at work. His works for me on my server. You should check this line and the id tags in the HTML file. Dim postMess As String = "Username=MyusernameHere&Password=MyPasswordHe re" Please find the HTML tag id for the "Username" and for the "Password" on your site. I have a problem reading that HTML file, it's quite messy, and I don't have all night to read out those id's from your generated HTML file. I have edited the following lines so you will get an output. Dim line As String = sReader.ReadLine() While (Not line Is Nothing) System.Console.WriteLine(line) line = sReader.ReadLine() End While Please use Byte in VB.Net. I am getting the error of either "TimeOut Expired" or "Underlying Connection Sounds like a network problem, not a code problem. This VB.NET code works for me: Module Module1 Sub Main() Dim request As System.Net.WebRequest = System.Net.WebRequest.Create("https://myServerHere") ' Post login request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" Dim streamOut As System.IO.Stream = request.GetRequestStream() Dim postMess As String = "Username=MyusernameHere&Password=MyPasswordHe re" Dim byt As Byte() = System.Text.Encoding.UTF8().GetBytes(postMess) streamOut.Write(byt, 0, byt.Length) streamOut.Close() ' Get the response stream Dim TheResponse As System.Net.WebResponse = request.GetResponse() ' Write the headers System.Console.WriteLine(TheResponse.Headers().ToS tring()) ' Get the connection stream Dim stream As System.IO.Stream = TheResponse.GetResponseStream() ' Using a stream reader (to read a complete line etc) Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(stream) 'Outputs all lines from the stream Dim line As String = sReader.ReadLine() While (Not line Is Nothing) System.Console.WriteLine(line) line = sReader.ReadLine() End While ' Close the connection TheResponse.Close() End Sub End Module Regards, Lars-Inge Tønnessen |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |