473,320 Members | 1,884 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,320 software developers and data experts.

WebRequest Help Please

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.O pen);

postStream = f;

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

Stream streamResponse = response.GetResponseStream();
Mar 12 '06 #1
6 2439
What do you mean by 'locks up'? You set the timeout property. It should
either throw an excepiton or get the response.

Mar 12 '06 #2
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.O pen);

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,
--
Joerg Jooss
ne********@joergjooss.de
Mar 12 '06 #3
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

"Andy" <pp****@gmail.com> wrote in message
news:11**********************@j52g2000cwj.googlegr oups.com...
What do you mean by 'locks up'? You set the timeout property. It should
either throw an excepiton or get the response.

Mar 12 '06 #4
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
"Joerg Jooss" <ne********@joergjooss.de> wrote in message
news:94**************************@msnews.microsoft .com...
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.O pen);

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,
--
Joerg Jooss
ne********@joergjooss.de

Mar 12 '06 #5
Bob
Hi,
Now working.
Thanks
bob
"Bob" <bo*@nowhere.com> wrote in message
news:eb****************@tk2msftngp13.phx.gbl...
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.O pen);

postStream = f;

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

Stream streamResponse = response.GetResponseStream();

Mar 13 '06 #6
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,
--
Joerg Jooss
ne********@joergjooss.de
Mar 13 '06 #7

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

Similar topics

2
by: Keith Selbee | last post by:
I am trying to submit data to a webpage in the form of a post and my code is below. It is a function that takes a url and the post content as strings and then performs the post. But as soon as I...
5
by: Tony | last post by:
Please help, I've scoured the web but cannot find an answer to this! I want to use WebRequest to get the contents of a web page. The trouble is, the page require you to go to a login page (using...
2
by: Tony | last post by:
Hi all, I need some help please using WebRequest with a page that requires you to log in through a different page. I have a roster that is located at our internal address of...
12
by: ThyRock | last post by:
I am working on a WebRequest accessing the US Postal Service WebTools test API. This service uses a DLL file (ShippingAPITest.dll) with a query string which includes XML. The web service accepts...
0
by: buran | last post by:
Dear ASP.NET Programmers, Please consider the intranet site with the following forms authentication page http://localhost/database/login.aspx. The usernames and passwords are stored in the SQL...
0
by: Gordon | last post by:
I use the following code to get source HTML. The second line seems to work when I get no response from a site. However, I want to stop the request if it's taking more than 20 - 30 seconds. I can't...
2
by: kkb | last post by:
Hello! First, I'm sorry because of my english... I'll try to be understandable! I've got a strange problem using .NET 2003 C# and I haven't figured it out for a long time. I'm implementing an...
6
by: Jack | last post by:
I have a WebRequest object that I use to log into a site and then post some XML. In doing this I set the KeepAlive = true so that it maintains the connection and does operates undo the initial...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm trying to create a call to a web page to validate and register software. The code I'm using is: Private Sub OK_Click(ByVal sender As...
3
by: Dave | last post by:
string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Which works...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.