HighTechTalks DotNet Forums  

How to Create HTTPS Connection From J#

Dotnet VJSharp microsoft.public.dotnet.vjsharp


Discuss How to Create HTTPS Connection From J# in the Dotnet VJSharp forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
BillHouse
 
Posts: n/a

Default How to Create HTTPS Connection From J# - 02-22-2005 , 08:13 PM






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


Reply With Quote
  #2  
Old   
BillHouse
 
Posts: n/a

Default Re: How to Create HTTPS Connection From J# - 02-25-2005 , 03:49 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!

BH

Reply With Quote
  #3  
Old   
neerajb (AT) noida (DOT) nospamhcltech.com
 
Posts: n/a

Default Re: How to Create HTTPS Connection From J# - 05-17-2005 , 01:20 AM



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



"Lars-Inge Tønnessen [VJ# MVP]" wrote:

Quote:
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




Reply With Quote
  #4  
Old   
neerajb (AT) noida (DOT) nospamhcltech.com
 
Posts: n/a

Default Re: How to Create HTTPS Connection From J# - 05-19-2005 , 01:03 AM



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:

Quote:
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




Reply With Quote
  #5  
Old   
neerajb (AT) noida (DOT) nospamhcltech.com
 
Posts: n/a

Default Re: How to Create HTTPS Connection From J# - 05-19-2005 , 01:22 AM



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().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 */


"neerajb (AT) noida (DOT) nospamhcltech.com" wrote:

Quote:
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




Reply With Quote
  #6  
Old   
neerajb (AT) noida (DOT) nospamhcltech.com
 
Posts: n/a

Default Re: How to Create HTTPS Connection From J# - 05-23-2005 , 02:59 AM



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

"Lars-Inge Tønnessen [VJ# MVP]" wrote:

Quote:
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




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.