473,772 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 NetworkCredenti al 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.Crea te(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", "applicatio n/xml");

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

try

{

r.Method = "POST";

Stream postStream = r.GetRequestStr eam();


FileStream f;

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

postStream = f;

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

Stream streamResponse = response.GetRes ponseStream();
Mar 12 '06 #1
6 2474
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 NetworkCredenti al 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 NetworkCredenti al 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.Crea te(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", "applicatio n/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.GetRequestStr eam();

FileStream f;

f = new FileStream("C:\ \testvend.xml", System.IO.FileM ode.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.GetRes ponseStream();


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********@joer gjooss.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 NetworkCredenti al class the right mechanism for doing this?
thanks
Bob
regards
Bob

"Andy" <pp****@gmail.c om> wrote in message
news:11******** **************@ j52g2000cwj.goo glegroups.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********@joe rgjooss.de> wrote in message
news:94******** *************** ***@msnews.micr osoft.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 NetworkCredenti al 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 NetworkCredenti al 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.Crea te(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", "applicatio n/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.GetRequestStr eam();

FileStream f;

f = new FileStream("C:\ \testvend.xml", System.IO.FileM ode.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.GetRes ponseStream();
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********@joer gjooss.de

Mar 12 '06 #5
Bob
Hi,
Now working.
Thanks
bob
"Bob" <bo*@nowhere.co m> wrote in message
news:eb******** ********@tk2msf tngp13.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 NetworkCredenti al 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.Crea te(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", "applicatio n/xml");

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

try

{

r.Method = "POST";

Stream postStream = r.GetRequestStr eam();


FileStream f;

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

postStream = f;

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

Stream streamResponse = response.GetRes ponseStream();

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 NetworkCredenti als.
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********@joer gjooss.de
Mar 13 '06 #7

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

Similar topics

2
3277
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 add the post data to the headers I get an exception that just says "headers". Can anyone please help me here? Thanks.... public string Get(string u, string c) { WebRequest wr = WebRequest.Create(u); wr.Headers.Add(c);
5
17577
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 ASP - not ASP.NET), and enter a username and password to get in. How do I use WebRequest in this situation? I cannot find any way of supplying the username and password. The '.Credentials' does not work because that is only for Windows...
2
2616
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 http://wfmmel01/WebETC/schedule.view.asp?. If you type this address in directly, you are taken to a login page where you enter your username and password. This address is http://wfmmel01/WebETC/employee.login.asp?intErrNumber=501. The HTML for the form looks like...
12
2867
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 the query string with no url encoding. I must pass the <> characters as they are in the query string. If these characters are url encoded the service rejects the request. This API Url is http://testing.shippingapis.com/ShippingAPITest.dll
0
1167
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 Server database. After authenticating, the users are be able to save the generated documents (HTML output of the generated document) when they click on a button in http://localhost/database/medicalDocs/1.aspx. I use the following code: Dim wReq...
0
2219
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 seem to make it work. Please point out the mistakes that I have. Thanks code............. WebRequest = WebRequest.Create(URL) WebRequest.Timeout() = RunInfo.ConnectTimeout WebResponse = WebRequest.GetResponse 'WEBRESPONSE IS A PUBLIC OBJECT...
2
5973
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 application to download images using System.NET classes (webclient, webrequest) asynchronously behind proxy server. The reading method works like this: System.Net.WebClient client=new System.Net.WebClient();
6
10010
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 login. Is there a way to force this connection to close after I an done with it. What happens is that I may need to post the XML in several chunks due to size therefore I cannot set the KeepAlive to False when I post the XML and the way the code...
3
9597
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 System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim WebRequest As HttpWebRequest Dim instance As HttpWebRequest =...
3
8188
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 fine, but I need to set and send a cookie with the WebRequest. How do I do that?
0
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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,...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
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...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3
2850
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.