Solved

Sending HTTPS from a PDA

Posted on 2004-03-20
8
721 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
ID: 10643145
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
ID: 10646878
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
ID: 10646920
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:astrofunkunator
ID: 10655233
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
 
LVL 12

Expert Comment

by:esteban_felipe
ID: 10655518
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
ID: 10665214

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
ID: 10674198
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
ID: 10674590
Where does the exception get raised? What's the complite exception info?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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