How to connect to Web Server with certificate?

  • Thread starter Thread starter jitsu
  • Start date Start date
J

jitsu

Hi all,

I have a problem. I need to connect to Web Server, send a xml string
(query) to it and receive another xml string (result).
I have the location of the Web Server like: https://extranet....com:8443,
then user name and password,
then I have the certifiace (I have already installed it to my
computer) and
I know the xml query string like:

<invoicing ver="1" action="abc">
<ID>155</ID>
</invoicing>

Could someone help plese?

Jitsu
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;

....

X509Store store = new X509Store(StoreName.My,
StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly |
OpenFlags.OpenExistingOnly);

//add reference to System.Security
X509Certificate2Collection certs =

X509Certificate2UI.SelectFromCollection(store.Certificates,
"Certificates", "Please select certificate to
use", X509SelectionFlag.SingleSelection);

HttpWebRequest request = WebRequest.Create("https://
extranet.com:8443");
string queryString = "<invoicing ver=\"1\" action=\"abc
\"><ID>155</ID></invoicing>";

request.ClientCertificates.Add(certs[0]);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = queryString.Length;
request.Timeout = 10000;

BinaryWriter tempBinaryWriter = new
BinaryWriter(request.GetRequestStream());
tempBinaryWriter.Write(queryString);
tempBinaryWriter.Flush();

StreamReader streamReader = new
StreamReader(request.GetResponse().GetResponseStream());
string response = streamReader.ReadToEnd();

Hope this helps :)
 
Many thanks for the response. I didn't have time to try it until
tomorrow,
but when I had tried it I received an error message:

"The name 'X509Certificate2UI' does not exist in the current context"

Do you have any Idea where could be the problem?

Jitsu



Miroslav Stampar napsal:
 
you have to add reference to System.Security manually (right click on
References -> Add reference -> System.Security).


(e-mail address removed) je napisao/la:
Many thanks for the response. I didn't have time to try it until
tomorrow,
but when I had tried it I received an error message:

"The name 'X509Certificate2UI' does not exist in the current context"

Do you have any Idea where could be the problem?

Jitsu



Miroslav Stampar napsal:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;

...

X509Store store = new X509Store(StoreName.My,
StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly |
OpenFlags.OpenExistingOnly);

//add reference to System.Security
X509Certificate2Collection certs =

X509Certificate2UI.SelectFromCollection(store.Certificates,
"Certificates", "Please select certificate to
use", X509SelectionFlag.SingleSelection);

HttpWebRequest request = WebRequest.Create("https://
extranet.com:8443");
string queryString = "<invoicing ver=\"1\" action=\"abc
\"><ID>155</ID></invoicing>";

request.ClientCertificates.Add(certs[0]);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = queryString.Length;
request.Timeout = 10000;

BinaryWriter tempBinaryWriter = new
BinaryWriter(request.GetRequestStream());
tempBinaryWriter.Write(queryString);
tempBinaryWriter.Flush();

StreamReader streamReader = new
StreamReader(request.GetResponse().GetResponseStream());
string response = streamReader.ReadToEnd();

Hope this helps :)
 
Thank you for quick response :)
But I have another (probably silly) question (and sorry for that,
because I have just started working with C#).

In my case it seems theat I am not able to add certificate to
WebRequest, I got following error message:

ClientCertificates' is not a member of 'System.Net.WebRequest

Is it another wrong reference?

Thank you for your advice.

Jitsu



you have to add reference to System.Security manually (right click on
References -> Add reference -> System.Security).

(e-mail address removed) je napisao/la:


Many thanks for the response. I didn't have time to try it until
tomorrow,
but when I had tried it I received an error message:
"The name 'X509Certificate2UI' does not exist in the current context"
Do you have any Idea where could be the problem?

Miroslav Stampar napsal:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
...
X509Store store = new X509Store(StoreName.My,
StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly |
OpenFlags.OpenExistingOnly);
//add reference to System.Security
X509Certificate2Collection certs =
X509Certificate2UI.SelectFromCollection(store.Certificates,
"Certificates", "Please select certificate to
use", X509SelectionFlag.SingleSelection);
HttpWebRequest request = WebRequest.Create("https://
extranet.com:8443");
string queryString = "<invoicing ver=\"1\" action=\"abc
\"><ID>155</ID></invoicing>";
request.ClientCertificates.Add(certs[0]);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = queryString.Length;
request.Timeout = 10000;
BinaryWriter tempBinaryWriter = new
BinaryWriter(request.GetRequestStream());
tempBinaryWriter.Write(queryString);
tempBinaryWriter.Flush();
StreamReader streamReader = new
StreamReader(request.GetResponse().GetResponseStream());
string response = streamReader.ReadToEnd();
Hope this helps :)- Hide quoted text -

- Show quoted text -
 
Hi all,
Finally it is working, thanks a lot for showing me the right way :)
(I had some problems with compilation in csharp, so I had to rewrite
into vb)


Imports System
Imports System.Security
Imports System.Security.Cryptography.X509Certificates
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Net
Imports System.Net.WebClient
Imports System.IO


Public Class Form1


Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
System.Net.ServicePointManager.CertificatePolicy = New
AcceptAllCertificatePolicy


Dim userString As String = "UserName"
Dim passwString As String = "Password123"
Dim userpaswString As String = userString & ":" & passwString
Dim queryString As String = "<ping/>"


Dim AuthBytes As Byte() =
System.Text.Encoding.ASCII.GetBytes(userpaswString)
Dim headerString As String =
Convert.ToBase64String(AuthBytes)


'Load the certificate from a file
Dim cert As X509Certificate2 = New X509Certificate2("C:
\cert.pfx", "CertPassw123")


Dim request As HttpWebRequest =
ttpWebRequest.Create("https://
extranet.mooa.com:8225")


request.ClientCertificates.Add(cert)
request.Method = "POST"
request.ContentType = "text/xml"
request.Headers.Add("Authorization: Basic " & headerString)
request.Timeout = 10000


'write data to request
Dim reqPostBuffer As Byte() =
System.Text.Encoding.UTF8.GetBytes(queryString)


request.ContentLength = reqPostBuffer.Length


Dim reqPostData As Stream = request.GetRequestStream()
reqPostData.Write(reqPostBuffer, 0, reqPostBuffer.Length)
reqPostData.Close()


Dim reqWebResponse As HttpWebResponse = request.GetResponse()


Dim enc As Encoding = System.Text.Encoding.UTF8


Dim reqResponseStream As StreamReader = New
reamReader(reqWebResponse.GetResponseStream(), enc)


Dim resHtml As String = reqResponseStream.ReadToEnd()


Debug.Print(resHtml)


reqWebResponse.Close()
reqResponseStream.Close()


End Sub


Private Class AcceptAllCertificatePolicy
Implements System.Net.ICertificatePolicy


Public Function CheckValidationResult(ByVal srvPoint As
System.Net.ServicePoint, ByVal certificate As
System.Security.Cryptography.X509Certificates.X509Certificate, ByVal
request As System.Net.WebRequest, ByVal certificateProblem As
Integer)
As Boolean Implements
System.Net.ICertificatePolicy.CheckValidationResult
Return True
End Function
End Class


End Class











Thank you for quick response :)
But I have another (probably silly) question (and sorry for that,
because I have just started working with C#).

In my case it seems theat I am not able to add certificate to
WebRequest, I got following error message:

ClientCertificates' is not a member of 'System.Net.WebRequest

Is it another wrong reference?

Thank you for your advice.

Jitsu

you have to add reference to System.Security manually (right click on
References -> Add reference -> System.Security).
(e-mail address removed) je napisao/la:
Many thanks for the response. I didn't have time to try it until
tomorrow,
but when I had tried it I received an error message:
"The name 'X509Certificate2UI' does not exist in the current context"
Do you have any Idea where could be the problem?
Jitsu
Miroslav Stampar napsal:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
...
X509Store store = new X509Store(StoreName.My,
StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly |
OpenFlags.OpenExistingOnly);
//add reference to System.Security
X509Certificate2Collection certs =
X509Certificate2UI.SelectFromCollection(store.Certificates,
"Certificates", "Please select certificate to
use", X509SelectionFlag.SingleSelection);
HttpWebRequest request = WebRequest.Create("https://
extranet.com:8443");
string queryString = "<invoicing ver=\"1\" action=\"abc
\"><ID>155</ID></invoicing>";
request.ClientCertificates.Add(certs[0]);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = queryString.Length;
request.Timeout = 10000;
BinaryWriter tempBinaryWriter = new
BinaryWriter(request.GetRequestStream());
tempBinaryWriter.Write(queryString);
tempBinaryWriter.Flush();
StreamReader streamReader = new
StreamReader(request.GetResponse().GetResponseStream());
string response = streamReader.ReadToEnd();
Hope this helps :)- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Back
Top