?
Solved

How to send the request to some url (jsp or asp page) and receive the response back from C#?

Posted on 2005-04-18
7
Medium Priority
?
247 Views
Last Modified: 2010-04-16
Hi,

I need from C# (ASP.NET) to send request to url and receive the reply syncronyously

In C++ there is winniet
in Java there HttpConnection class

what C# suggests?
0
Comment
Question by:netformx
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 13808864
What's the final idea? To get the page, parse it or otherwise process it and show a result? If so, you can use sockets, open a socket to the other page, send the HTTP request, receive the response, parse it/process it.
0
 

Author Comment

by:netformx
ID: 13808909
the final idea is get html and display it (Response.write(html) - it has to be by this way.

can you show code snippet, please?
0
 
LVL 7

Accepted Solution

by:
Ceiled earned 2000 total points
ID: 13809383
The easiest way is probably to use the System.Net.WebClient object, like so:

      WebClient wc = new WebClient();
      using (TextReader tr = new StreamReader(wc.OpenRead("http://www.google.com")))
      {
            Response.Write(tr.ReadToEnd());
      }

WebClient has a number of very simple methods for retrieving data from and sending data to web sites. If all you need is to retrieve the HTML from a page and display it, this should work fine for you.

If you're looking for a little more control about what actually gets sent to the server, you'll want to look at the WebRequest and WebResponse classes (for which WebClient is a wrapper). The equivalent of the above code using WebRequest and WebResponse would look something like this:

            WebRequest req = WebRequest.Create("http://www.google.com");
            WebResponse resp = req.GetResponse();
            using (TextReader tr = new StreamReader(resp.GetResponseStream()))
            {
                  Console.WriteLine(tr.ReadToEnd());
            }

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:TheAvenger
ID: 13809897
The solution of Ceiled seams to be much more effective than mine.
0
 
LVL 1

Expert Comment

by:sourabhkothari
ID: 13814247
if what you want is to display the html pages from the web than you can use the webbrowser control.
just call the navigate method and it'll show the webpage in the browser.

or for specifically http webrequest you can use the httpwebrequest and httpwebresponse classes.
works almost the same as webrequest.

Skot
0
 
LVL 7

Expert Comment

by:Ceiled
ID: 13816578
He specifically said that he needs to retrieve the HTML at a URL so he can Response.Write() it -- he's writing a web application himself, not displaying a web page in a rich client.

Also, HttpWebRequest is not intended to be used directly. It inherits from WebRequest, and if you call WebRequest.Create() with an HTTP address, an HttpWebRequest will be returned. If you need to look at a property that's specific to HTTP (like the StatusCode property of the response), then you can cast the request or response to do so. However, if you don't need to look at any properties that are specific to HTTP, it's really best to use the generic types so that you can easily use different protocols if necessary.
0
 

Author Comment

by:netformx
ID: 13858878
the solution gave exactly what I needed

thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question