all groups > vj# > february 2005 >
You're in the

vj#

group:

How to Create HTTPS Connection From J#



How to Create HTTPS Connection From J# BillHouse
2/22/2005 5:13:03 PM
vj#: I have a need to access a 3rd-party site via HTTPS from a service-side
process. They have example code written in Java, which all works in J#
except the HTTPS connection. For that, they reference sun.* packages that I
don't think J# supports. Is there an alternative approach I can use from J#?
Here is the Java code I'm trying to port to J#:

private HttpURLConnection createConnection()
{
// Create the URL class
URL url = null;

try
{
url = new URL(HTTPS_URL);
}
catch (MalformedURLException e)
{// THIS IS THE EXCEPTION THROWN
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(url == null)
{
return null;
}
}

// Create the connection the server
URLConnection connection = null;

try
{
connection = url.openConnection();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (NullPointerException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(connection == null)
{
return null;
}
}

// Configure the Http connection
HttpURLConnection httpConnection = null;

httpConnection = (HttpURLConnection)(connection);
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);

try
{
httpConnection.setRequestMethod("POST");
}
catch (ProtocolException e)
{
e.printStackTrace();
}

return httpConnection;
}
Re: How to Create HTTPS Connection From J# BillHouse
2/25/2005 12:49:01 PM
Yes, the .NET way is easy. I dumped the Java code and just did it myself in
VB.NET. I was hoping that there was a way to sort of preserve the
'Javaness' of the thing, since all their programmers are Java-folk, but it
really just isn't worth the bother. :-)

Thanks for the helpful reply!

Re: How to Create HTTPS Connection From J# Lars-Inge Tønnessen [VJ# MVP]
2/25/2005 8:30:30 PM

In .NET is very simple: Only 1 line of code: =:o)


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

Re: How to Create HTTPS Connection From J# neerajb NO[at]SPAM noida.nospamhcltech.com
5/16/2005 10:20:01 PM
Hi Tønnessen ,

I am trying this code for one site which is on HTTPS but it's giving the error

I have not understood the meaning of this line in your code
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.

Also do we need to pass any special parameters for HTTPS site or it is
samefro both HTTP or HTTPS site.

I am trying for this site https://corpmis.ggn.hcltech.com/

I am having valid username and password and trying to post the form.

Please HELP...........

Thanks & Regards,
Neeraj



[quoted text, click to view]
Re: How to Create HTTPS Connection From J# Lars-Inge Tønnessen [VJ# MVP]
5/18/2005 8:54:30 PM
[quoted text, click to view]

Hello, :o)


[quoted text, click to view]


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.


[quoted text, click to view]


Writing "https://" should do the trick for a secure connection.


[quoted text, click to view]

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();
}
}


[quoted text, click to view]

Did this help ?



Best Regards,
Lars-Inge Tønnessen

Re: How to Create HTTPS Connection From J# neerajb NO[at]SPAM noida.nospamhcltech.com
5/18/2005 10:03:53 PM
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().ToString())

' 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 */
[quoted text, click to view]
Re: How to Create HTTPS Connection From J# neerajb NO[at]SPAM noida.nospamhcltech.com
5/18/2005 10:22:01 PM
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 of either "TimeOut Expired" or "Underlying Connection
is Closed" 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().ToString())

' 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 */


[quoted text, click to view]
Re: How to Create HTTPS Connection From J# Lars-Inge Tønnessen [VJ# MVP]
5/19/2005 10:45:59 PM

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=MyPasswordHere"

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.

[quoted text, click to view]

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=MyPasswordHere"
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().ToString())

' 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

Re: How to Create HTTPS Connection From J# neerajb NO[at]SPAM noida.nospamhcltech.com
5/22/2005 11:59:30 PM
Hi Tønnessen ,
Thanks for the quick reply.

I am still getting the "The operation has timed out" error at the following
line of Code which is only 4th line in code given by you.

Dim streamOut As System.IO.Stream = request.GetRequestStream()

I am using following URL:


System.Net.WebRequest.Create("https://corpmis.ggn.hcltech.com//names.nsf?Login")

Is it because of nsf page i.e Lotus Notes or what may be the problem.

I am able to access this site from web browser so it is not the network
which is causing the problem.

Please help!!!!!
Thanks in Advance!!!!

Regards,
Neeraj

[quoted text, click to view]
Re: How to Create HTTPS Connection From J# Lars-Inge Tønnessen [VJ# MVP]
5/24/2005 12:00:00 AM

[quoted text, click to view]

Sorry, I don't have a login to that site so I can't test it. The example
works on my server.


[quoted text, click to view]

No, the VB.NET client acts as any web browsers as far as the server knows
it. If you POST the correct commands to the web server, it should act
accordingly.

Any firewalls/anti virus programs blocking the VB.NET application?


Regards,
Lars-Inge Tønnessen

AddThis Social Bookmark Button