Solved

Sending HTTPS from a PDA

Posted on 2004-03-20
8
707 Views
Last Modified: 2011-09-20
Hi there,

I am using the following code to send via HTTP but I would rather send via HTTPS, if i change the http to https it crashes. Any healp would be wonderful..

// Create a new 'HttpWebRequest' object to the mentioned Uri.                
                              HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://xxx.xxx.xxx.xx/cgi-bin/pda/inbound");                        
                                                            myHttpWebRequest.AllowWriteStreamBuffering = false;
                        
                              string postData = fileLine;
                              
                              // Set 'Method' property of 'HttpWebRequest' class to POST.
                              myHttpWebRequest.Method = "POST";
                              
                              ASCIIEncoding encodedData = new ASCIIEncoding();
                              
                              byte[]  byteArray=encodedData.GetBytes(postData);
                              
                              // Set 'ContentType' property of the 'HttpWebRequest' class to "application/x-www-form-urlencoded".
                              myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
                              
                              // If the AllowWriteStreamBuffering property of HttpWebRequest is set to false,the contentlength has to be set to length of data to be posted else Exception(411) is raised.
                              myHttpWebRequest.ContentLength = byteArray.Length;
                              
                              Stream newStream=myHttpWebRequest.GetRequestStream();
                              
                              newStream.Write(byteArray,0,byteArray.Length);
                              
                              newStream.Close();
                              
                              // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
                              HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
0
Comment
Question by:astrofunkunator
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:esteban_felipe
Comment Utility
Hi astrofunkunator,

what do you mean with "it crashes"? what exception is it throwing?. That information will be useful to find out a solution

Esteban Felipe
www.estebanf.com
0
 

Author Comment

by:astrofunkunator
Comment Utility
Sorry the error returned is : System.Net.WebException:Could not establish trust relationship with remote server.

Cheers

A.
0
 
LVL 12

Accepted Solution

by:
esteban_felipe earned 125 total points
Comment Utility
K, we can start from there...

This problem is ussually associated with the site ssl certificate. Tell me something: if you access the site (with https, of course) using a browser, do you see any popup or message related to the certificate?

There's a common workaround for this issue, but it's not the most secure. It's based in create a new certificatepolicy that accepts all certificate. Here's a class that you can use:
public class TrustThemAll: System.Net.ICertificatePolicy
{
 public TrustThemAll()
 {}

 public bool CheckValidationResult(ServicePoint sp,
  X509Certificate cert,WebRequest req, int problem)
 {
  return true;
 }
}

Then in your code, before declaring the HttpWebRequest you would do:
ServicePointManager.CertificatePolicy = new TrustThemAll();
HttpWebRequest ........ //your code continues here.

Check the site using your browser and implement this workaround. Let me know if you get any good result.

Good luck :)

Esteban Felipe
www.estebanf.com
0
 

Author Comment

by:astrofunkunator
Comment Utility
Thanks for the suggestion. I implemented your change but unfortunately I am still receiving the same error. System.net.WebException : The remote name could not be resolved.

When i try and access the cgi through a browser it works fine, no cert messages, just the standard "No document received."

Regards
Marcus
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:esteban_felipe
Comment Utility
But the exception changed. It used to be: "Could not establish trust relationship with remote server" and now it's  "The remote name could not be resolved". Do you have access from the pda to the site? and what do you mean with "the standard no document recieved"? is the target page expecting something?
0
 

Author Comment

by:astrofunkunator
Comment Utility

Sorry my mistake, changed to the web address rather then the IP and it works a treat. Thank you very very much for you assistance..

Regards

Marcus
0
 

Author Comment

by:astrofunkunator
Comment Utility
Still not working!!

It works once then i get the  "A blocking operation is currently excuting"!! I close the response and the stream??

What is there left to close?

// Ensure the trust relation ship for
ServicePointManager.CertificatePolicy = new TrustThemAll();


// Create a new 'HttpWebRequest' object to the mentioned Uri.                
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create   ("https://203.174.144.37/cgi-bin/pda/inbound");                        

// Set AllowWriteStreamBuffering to 'false'.
myHttpWebRequest.AllowWriteStreamBuffering = false;

myHttpWebRequest.KeepAlive = false;
                        
string postData = fileLine;
                              
Set 'Method' property of 'HttpWebRequest' class to POST.
myHttpWebRequest.Method = "POST";

ASCIIEncoding encodedData = new ASCIIEncoding();
                              
byte[]  byteArray=encodedData.GetBytes(postData);

// Set 'ContentType' property of the 'HttpWebRequest' class to "application/x-www-form-urlencoded".

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

// If the AllowWriteStreamBuffering property of HttpWebRequest is set to false,the contentlength has to be set to length of data to be posted else Exception(411) is raised.

myHttpWebRequest.ContentLength = byteArray.Length;

Stream newStream=myHttpWebRequest.GetRequestStream();

newStream.Write(byteArray,0,byteArray.Length);

newStream.Close();

// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

myHttpWebResponse.Close();
0
 
LVL 12

Expert Comment

by:esteban_felipe
Comment Utility
Where does the exception get raised? What's the complite exception info?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now