WebRequest Help Please

B

Bob

Hi,
I am new to using a web resource which is not a webservice and I am
spinning my wheels with the webrequest class.
I am trying to interact with a URI that has been given to me.
It is say www.bloggs.com/cgi-bin/thin-client"

My app gathers data and is supposed to pass this data to the URI which will
then respond with a result.
So some real basic questions.
1)They have given me a user name and password to use.
Where does this fit into the construction of the request? I thought it
might be using the NetworkCredential class and the CredentialCache but I
couldn't even get the basic microsoft example to work.

2) I have an example printed request which shows the header and body.
So I made an XML doc the same as the body, and tried to incorporate that
into the request using the GetRequest stream method.

I assumed that it would be rejected as I didn't have any credentials.
But it just locks up at the GetResponse

my code follows:
Thanks
Bob

WebRequest r =
WebRequest.Create(http://www.blogs.com//cgi-bin//thin-client);

r.Headers.Set("Method","POST");

r.Headers.Set("URI", "/cgi-bin/thin-client");

r.Headers.Set("HTTP-version", "HTTP/1.1");



r.Headers.Set("UserAgent", "specialagent"); // not the real agent

r.Headers.Set("ContentLength", "310");

r.Headers.Set("ContentType", "application/xml");

r.Headers.Set("Timeout", "10000");

try

{

r.Method = "POST";

Stream postStream = r.GetRequestStream();




FileStream f;

f = new FileStream("C:\\testvend.xml",System.IO.FileMode.Open);



postStream = f;

WebResponse response = r.GetResponse(); *****LOCKS UP HERE*********

Stream streamResponse = response.GetResponseStream();
 
A

Andy

What do you mean by 'locks up'? You set the timeout property. It should
either throw an excepiton or get the response.
 
J

Joerg Jooss

Thus wrote Bob,
Hi,
I am new to using a web resource which is not a webservice and I am
spinning my wheels with the webrequest class.
I am trying to interact with a URI that has been given to me.
It is say www.bloggs.com/cgi-bin/thin-client"
My app gathers data and is supposed to pass this data to the URI which
will
then respond with a result.
So some real basic questions.
1)They have given me a user name and password to use.
Where does this fit into the construction of the request? I thought
it
might be using the NetworkCredential class and the CredentialCache but
I
couldn't even get the basic microsoft example to work.

The question is what kind of authentication the server uses. Not in all cases
is NetworkCredential applicable.
2) I have an example printed request which shows the header and body.
So I made an XML doc the same as the body, and tried to incorporate
that
into the request using the GetRequest stream method.
I assumed that it would be rejected as I didn't have any credentials.
But it just locks up at the GetResponse

my code follows:
Thanks
Bob
WebRequest r =
WebRequest.Create(http://www.blogs.com//cgi-bin//thin-client);
r.Headers.Set("Method","POST");

r.Headers.Set("URI", "/cgi-bin/thin-client");

r.Headers.Set("HTTP-version", "HTTP/1.1");

r.Headers.Set("UserAgent", "specialagent"); // not the real agent

r.Headers.Set("ContentLength", "310");

r.Headers.Set("ContentType", "application/xml");

r.Headers.Set("Timeout", "10000");

Did you really care to take a look at any code sample? All of the above headers
are set through properties on HttpWebRequest, save for URI and HTTP-Version,
which aren't HTTP headers. Setting a fixed Content-Length implies that the
content is really 310 bytes long.
try

{

r.Method = "POST";

Stream postStream = r.GetRequestStream();

FileStream f;

f = new FileStream("C:\\testvend.xml",System.IO.FileMode.Open);

postStream = f;

You have to copy your data from f to postStream. Simply reassigning a variable
won't cut it ;-)
WebResponse response = r.GetResponse(); *****LOCKS UP HERE*********

Stream streamResponse = response.GetResponseStream();

Wrap the calls to disposable resources in using blocks -- you don't close
your disposable resources.

Both request and response stream *must* be closed in order to avoid resource
leaks. An open request stream will block your method, because HttpWebRequest
has no clue that you're done sending data.

Cheers,
 
B

Bob

Hi Andy,
The getresponse instruction executes and never returns. If you do a debugger
breakall the pointer is at the Getresponse still. I guess 'Locks up' is a
bit melodramatic.
I think my real problem is twofold.
1) I don't understand how to post an XML doc.
All of the examples show a byte array being sent.
Do you know of any examples of this?

2) Once I get the posting mechanism working (ie I am getting rejected by the
site) I need to incorporate the User / password that I have been given. Is
the NetworkCredential class the right mechanism for doing this?
thanks
Bob
regards
Bob
 
B

Bob

Hi,
Joerg,
Thanks for your response.
I assure you I have looked at heaps of examples. All I have been able to
find is the sending of byte arrays
I didn't bother putting the 'closing code' in my example as it is not
relevant.

Essentially I am looking for:
1) The correct industrial strength way of posting an XML doc to a non
framework webserver.
2) The correct method of presenting my user name and password.
I have no feel for what is good coding practise in this area, and would love
to see an example of how it is done.
regards
Bob
 
J

Joerg Jooss

Thus wrote Bob,
Hi,
Joerg,
Thanks for your response.
I assure you I have looked at heaps of examples. All I have been able
to
find is the sending of byte arrays
I didn't bother putting the 'closing code' in my example as it is not
relevant.

I beg to differ. A significant number of problems posted by users are simply
caused by broken or non-existing resource management. Also, using blocks
make explicit calls Close() or Dispose() unnecessary.
Essentially I am looking for:
1) The correct industrial strength way of posting an XML doc to a non
framework webserver.

What's a non framework web server?

Anyway, I've posted numerous samples in this group, e.g. http://tinyurl.com/zq8y3

2) The correct method of presenting my user name and password.
I have no feel for what is good coding practise in this area, and
would love
to see an example of how it is done.

As I said before, you have to know what kind of authentication is being used
on the server side. HTTP authentication schemes work wirh NetworkCredentials.
Forms authentication (pretty much standard in Internet web apps) require
form fields for user name and password to be posted to the server.

Cheers,
 

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