Solved

Sending HTTPS from a PDA

Posted on 2004-03-20
8
717 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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