473,800 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10750
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Net;
using System.IO;
using System.Security .Cryptography.X 509Certificates ;

....

X509Store store = new X509Store(Store Name.My,
StoreLocation.C urrentUser);
store.Open(Open Flags.ReadOnly |
OpenFlags.OpenE xistingOnly);

//add reference to System.Security
X509Certificate 2Collection certs =

X509Certificate 2UI.SelectFromC ollection(store .Certificates,
"Certificat es", "Please select certificate to
use", X509SelectionFl ag.SingleSelect ion);

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

request.ClientC ertificates.Add (certs[0]);
request.Method = "POST";
request.Content Type = "text/xml";
request.Content Length = queryString.Len gth;
request.Timeout = 10000;

BinaryWriter tempBinaryWrite r = new
BinaryWriter(re quest.GetReques tStream());
tempBinaryWrite r.Write(querySt ring);
tempBinaryWrite r.Flush();

StreamReader streamReader = new
StreamReader(re quest.GetRespon se().GetRespons eStream());
string response = streamReader.Re adToEnd();

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 'X509Certificat e2UI' 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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Net;
using System.IO;
using System.Security .Cryptography.X 509Certificates ;

...

X509Store store = new X509Store(Store Name.My,
StoreLocation.C urrentUser);
store.Open(Open Flags.ReadOnly |
OpenFlags.OpenE xistingOnly);

//add reference to System.Security
X509Certificate 2Collection certs =

X509Certificate 2UI.SelectFromC ollection(store .Certificates,
"Certificat es", "Please select certificate to
use", X509SelectionFl ag.SingleSelect ion);

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

request.ClientC ertificates.Add (certs[0]);
request.Method = "POST";
request.Content Type = "text/xml";
request.Content Length = queryString.Len gth;
request.Timeout = 10000;

BinaryWriter tempBinaryWrite r = new
BinaryWriter(re quest.GetReques tStream());
tempBinaryWrite r.Write(querySt ring);
tempBinaryWrite r.Flush();

StreamReader streamReader = new
StreamReader(re quest.GetRespon se().GetRespons eStream());
string response = streamReader.Re adToEnd();

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 'X509Certificat e2UI' 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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Net;
using System.IO;
using System.Security .Cryptography.X 509Certificates ;

...

X509Store store = new X509Store(Store Name.My,
StoreLocation.C urrentUser);
store.Open(Open Flags.ReadOnly |
OpenFlags.OpenE xistingOnly);

//add reference to System.Security
X509Certificate 2Collection certs =

X509Certificate 2UI.SelectFromC ollection(store .Certificates,
"Certificat es", "Please select certificate to
use", X509SelectionFl ag.SingleSelect ion);

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

request.ClientC ertificates.Add (certs[0]);
request.Method = "POST";
request.Content Type = "text/xml";
request.Content Length = queryString.Len gth;
request.Timeout = 10000;

BinaryWriter tempBinaryWrite r = new
BinaryWriter(re quest.GetReques tStream());
tempBinaryWrite r.Write(querySt ring);
tempBinaryWrite r.Flush();

StreamReader streamReader = new
StreamReader(re quest.GetRespon se().GetRespons eStream());
string response = streamReader.Re adToEnd();

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:

ClientCertifica tes' is not a member of 'System.Net.Web Request

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.comwro te:
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 'X509Certificat e2UI' 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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Net;
using System.IO;
using System.Security .Cryptography.X 509Certificates ;
...
X509Store store = new X509Store(Store Name.My,
StoreLocation.C urrentUser);
store.Open(Open Flags.ReadOnly |
OpenFlags.OpenE xistingOnly);
//add reference to System.Security
X509Certificate 2Collection certs =
X509Certificate 2UI.SelectFromC ollection(store .Certificates,
"Certificat es", "Please select certificate to
use", X509SelectionFl ag.SingleSelect ion);
HttpWebRequest request = WebRequest.Crea te("https://
extranet.com:84 43");
string queryString = "<invoicing ver=\"1\" action=\"abc
\"><ID>155</ID></invoicing>";
request.ClientC ertificates.Add (certs[0]);
request.Method = "POST";
request.Content Type = "text/xml";
request.Content Length = queryString.Len gth;
request.Timeout = 10000;
BinaryWriter tempBinaryWrite r = new
BinaryWriter(re quest.GetReques tStream());
tempBinaryWrite r.Write(querySt ring);
tempBinaryWrite r.Flush();
StreamReader streamReader = new
StreamReader(re quest.GetRespon se().GetRespons eStream());
string response = streamReader.Re adToEnd();
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.X 509Certificates
Imports System.Collecti ons.Generic
Imports System.Componen tModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows. Forms
Imports System.Net
Imports System.Net.WebC lient
Imports System.IO
Public Class Form1
Private Sub Button1_Click(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click
System.Net.Serv icePointManager .CertificatePol icy = New
AcceptAllCertif icatePolicy
Dim userString As String = "UserName"
Dim passwString As String = "Password12 3"
Dim userpaswString As String = userString & ":" & passwString
Dim queryString As String = "<ping/>"
Dim AuthBytes As Byte() =
System.Text.Enc oding.ASCII.Get Bytes(userpaswS tring)
Dim headerString As String =
Convert.ToBase6 4String(AuthByt es)
'Load the certificate from a file
Dim cert As X509Certificate 2 = New X509Certificate 2("C:
\cert.pfx", "CertPassw1 23")
Dim request As HttpWebRequest =
ttpWebRequest.C reate("https://
extranet.mooa.c om:8225")
request.ClientC ertificates.Add (cert)
request.Method = "POST"
request.Content Type = "text/xml"
request.Headers .Add("Authoriza tion: Basic " & headerString)
request.Timeout = 10000
'write data to request
Dim reqPostBuffer As Byte() =
System.Text.Enc oding.UTF8.GetB ytes(queryStrin g)
request.Content Length = reqPostBuffer.L ength
Dim reqPostData As Stream = request.GetRequ estStream()
reqPostData.Wri te(reqPostBuffe r, 0, reqPostBuffer.L ength)
reqPostData.Clo se()
Dim reqWebResponse As HttpWebResponse = request.GetResp onse()
Dim enc As Encoding = System.Text.Enc oding.UTF8
Dim reqResponseStre am As StreamReader = New
reamReader(reqW ebResponse.GetR esponseStream() , enc)
Dim resHtml As String = reqResponseStre am.ReadToEnd()
Debug.Print(res Html)
reqWebResponse. Close()
reqResponseStre am.Close()
End Sub
Private Class AcceptAllCertif icatePolicy
Implements System.Net.ICer tificatePolicy
Public Function CheckValidation Result(ByVal srvPoint As
System.Net.Serv icePoint, ByVal certificate As
System.Security .Cryptography.X 509Certificates .X509Certificat e, ByVal
request As System.Net.WebR equest, ByVal certificateProb lem As
Integer)
As Boolean Implements
System.Net.ICer tificatePolicy. CheckValidation Result
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:

ClientCertifica tes' is not a member of 'System.Net.Web Request

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.comwro te:
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 'X509Certificat e2UI' 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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Net;
using System.IO;
using System.Security .Cryptography.X 509Certificates ;
...
X509Store store = new X509Store(Store Name.My,
StoreLocation.C urrentUser);
store.Open(Open Flags.ReadOnly |
OpenFlags.OpenE xistingOnly);
//add reference to System.Security
X509Certificate 2Collection certs =
X509Certificate 2UI.SelectFromC ollection(store .Certificates,
"Certificat es", "Please select certificate to
use", X509SelectionFl ag.SingleSelect ion);
HttpWebRequest request = WebRequest.Crea te("https://
extranet.com:84 43");
string queryString = "<invoicing ver=\"1\" action=\"abc
\"><ID>155</ID></invoicing>";
request.ClientC ertificates.Add (certs[0]);
request.Method = "POST";
request.Content Type = "text/xml";
request.Content Length = queryString.Len gth;
request.Timeout = 10000;
BinaryWriter tempBinaryWrite r = new
BinaryWriter(re quest.GetReques tStream());
tempBinaryWrite r.Write(querySt ring);
tempBinaryWrite r.Flush();
StreamReader streamReader = new
StreamReader(re quest.GetRespon se().GetRespons eStream());
string response = streamReader.Re adToEnd();
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
3158
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” ”cn=admin,ou=People,dc=test,dc=com" "secretje" AuthenticationTypes.ServerBin
4
6057
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 authenticated with the mainframe and if its ok, then the application proceeds using the generic application id. We are not able to track the user in the DB2 on Mainframe. Is there anyway I can pass the end userid along with the generic userid, so I...
0
2434
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 to send: Client Certificate and Username / password. We are using a Web Service provider which uses IIS 5.1 as HTTP server. IIS is set to require client certificate and Basic Authentication.
8
6453
by: Lenn | last post by:
Hello, I've been struggling with this for couple of days now. All I want to do is to enable SSL protocol on the webserver. I want to be able to generate and sign my own certificates. I used various tools to do that, such as makecert.exe from .NET SDK and even downloaded OpenSSL and generated certificates using that. I installed my own certificates on IIS, but SSL simply wont work with any of mine certificates. I get an error in server's...
0
1901
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 was closed: Could not establish secure channel for SSL/TLS. at System.Net.HttpWebRequest.CheckFinalStatus() at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult) at
4
2067
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 authentication and not "server authentification" The code i wrote is quite straight forward. (See listing below) With server authentication, the server's application will contain a certificate created specifically for that server. The client...
2
2395
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 = CType(WebRequest.Create(goSettings.NHSDPBClaimsURL), HttpWebRequest) nc = New NetworkCredential(goSettings.NHSUserName, goSettings.NHSPassword)
6
5114
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 with their corresponding employees(This is our clients requirement, so that they can later provide the backup of the database if the organization decides to move out). And for that what we intend to do is restore the backup of database which already...
5
4457
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: 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:
0
1599
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 am not in control of this server or web service). I can create a web reference using VS2005 with the https URL of the WSDL file. When I try sending the data I receive the below error: "Unable to connect to the remote server" I added the...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.