HttpWebRequest lockup while posting to a site 'accepting client certificates'

B

B Martin

Hello,

I am having some trouble posting xml to a site that is set up to accept
client certificates (as well as requires certificates). My program
will just hang forever when I try to add the xml to the request stream
(see code below) of the HttpWebRequest object. It hangs regardless of
whether I have added the certificate to the HttpWebRequest object, so I
do not believe it is my certificate causing this. I have the web site
I'm posting to set up to accept client certificates, require SSL, and
to authenticate by windows auth (all of which I need to do).

When I change either the authentication mode to anonymous or the client
certificates to ignore, the code below works fine. If I do not post
any data but to a GET instead, then the code will connect just fine
(and return the HTML). I've tried everything I could think of and it
will just not work.

There is definitely a deadlock issue here -- although I cannot
understand why the Write method is affected. Does anyone see anything
that I'm doing wrong here or has run into this before?

Thanks.

BM

=======================================================
string xml = "<ROOT>test</ROOT>";
string results = string.Empty;
Uri uri = new Uri("https://localhost/MyReceiver.aspx");


//create the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.ContentType = "text/xml";
request.Method = "POST";
request.KeepAlive = true;
request.Credentials = CredentialCache.DefaultCredentials;

//Set the certificate - load from file
request.ClientCertificates.Add(GetCertificate());

byte[] data = Encoding.ASCII.GetBytes(xml);
request.ContentLength = data.Length;
Stream dataStream = null;
try{
dataStream = request.GetRequestStream();

// Write the data to be posted.
dataStream.Write(data, 0, data.Length); //HANGS here
}
finally{
if (dataStream != null){
dataStream.Close();
dataStream = null;
}
}

HttpWebResponse response = null;
StreamReader reader = null;
try{
//Get the response
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
results = reader.ReadToEnd();
}
finally{
if (reader != null){
reader.Close();
reader = null;
}
if (response != null){
response.Close();
response = null;
}
}
 
B

B Martin

Thanks for the reply.

I am certain I have a valid certificate set up. I have used the cert
before on a call to a web service on my local machine. However I had
used the proxy to call the ws, not the HttpWebRequest -- that is why
I'm thinking that I'm not doing something right in the code. However,
the lockup happens even if I don't send a cert at all (with 'accepting
clent certs' set sending one should be optional).

I also have IIS set up to run as myself and I am an admin on the
machine so there should not be any problems accessing the cert store.
I also have a custom CertificatePolicy set up to accept all
certificates so it isn't the SSL cert. Is there anything else that
could cause this?

BM
 
A

Alvin Bruney [MVP]

what error are you getting back? set the debugger to break on all exceptions
and run the app.

--
Regards,
Alvin Bruney

Shameless Author plug
The Microsoft Office Web Components Black Book with .NET
http://tinyurl.com/27cok
 
B

B Martin

I don't get an error back. It just hangs. I have the debugger set to
break on all exceptions and stepped through it. As soon as I execute
this statement, the app freezes and the execution never returns from
the call.

dataStream.Write(data, 0, data.Length); //HANGS here

I have left it sitting there for three hours thinking it would
eventually return and it never did. It is definitely some sort of
deadlock situation, but I have no idea what is causing it (and why
writing to a stream of all things?).

BM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top