473,408 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to connect to Web Server with certificate?

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

May 24 '07 #1
5 4436
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.Cert ificates,
"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().GetResponseStre am());
string response = streamReader.ReadToEnd();

Hope this helps :)

May 24 '07 #2
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.Cert ificates,
"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().GetResponseStre am());
string response = streamReader.ReadToEnd();

Hope this helps :)
May 26 '07 #3
you have to add reference to System.Security manually (right click on
References -Add reference -System.Security).
j...@post.cz 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.Cert ificates,
"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().GetResponseStre am());
string response = streamReader.ReadToEnd();

Hope this helps :)
May 27 '07 #4
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

On May 27, 11:54 pm, "Miroslav Stampar [MCSD.NET / Security+]"
<miroslav.stam...@gmail.comwrote:
you have to add reference to System.Security manually (right click on
References -Add reference -System.Security).

j...@post.cz 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.Cert ificates,
"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().GetResponseStre am());
string response = streamReader.ReadToEnd();
Hope this helps :)- Hide quoted text -

- Show quoted text -

May 28 '07 #5
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.X509 Certificate, ByVal
request As System.Net.WebRequest, ByVal certificateProblem As
Integer)
As Boolean Implements
System.Net.ICertificatePolicy.CheckValidationResul t
Return True
End Function
End Class
End Class



On May 28, 2:21 pm, j...@post.cz wrote:
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

On May 27, 11:54 pm, "Miroslav Stampar [MCSD.NET / Security+]"

<miroslav.stam...@gmail.comwrote:
you have to add reference to System.Security manually (right click on
References -Add reference -System.Security).
j...@post.cz 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.Cert ificates,
"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().GetResponseStre am());
string response = streamReader.ReadToEnd();
Hope this helps :)- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

May 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jdelimon | last post by:
Hello I use the following code to connect to an openldap server This works just fine _deRoot = new DirectoryEntry "LDAP://ws244501.test.com/ou=People,dc=test,dc=comā€¯...
4
by: Wonderinguy | last post by:
Our websphere application uses a generic application userid to connect and query db2 on z/os via DB2 connect. The end user,logs in to the application using his regular userid, which is then...
0
by: Joey Edelstein | last post by:
Hi, We are trying to add a Client Certificate support for our web app that emulates a hardware device web app. The hardware uses a 2 factors authentication, which requires a Web Service client...
0
by: Subra Mallampalli | last post by:
Hi, I am trying to connect over HTTPS using a client certificate. I am able to connect fine when using IE. From my application, however, I get the following error: The underlying connection...
4
by: jens Jensen | last post by:
Hello, I was given the task to build a .Net client that will talk to IBM integration server via HTTP post. The idea is that each http packet exchange should be authenticated via X09 "client...
2
by: leeo | last post by:
This is driving me absolutely mad. We've been using this code to connect to a remote site: System.Net.ServicePointManager.CertificatePolicy = New cCertificatePolicy wr =...
6
by: mplpost | last post by:
Our web application requires that when a new Organization is created,a separate Database is to be created for the corresponding organization, so that each organization remains a separate entity...
5
by: jitsu | last post by:
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:...
0
by: WatchoutMac | last post by:
I have a C# .NET client (2.0 Framework) that needs to connect to a java web service (built with Axis) and send some data for processing. The java web service is hosted on a secured Apache server ( I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.