URL class in C#

K

K R

Hi,

Please let me know the equivalent code to the below code in c#
(ASP.NET).

This below code is written in java.

MyDataString =
#DREDOCID 1
#DREFIELDNAME Price
#DREFIELDVALUE 10
#DREDOCREF http://www.autonomy.com/autonomy/dynamic/autopage442.shtml
#DREFIELDNAME Country
#DREFIELDVALUE UK

SAMPLE CODE

URL suir_url = new URL("http://" +SUIRServer +":" +SUIRPort
+"/DREREPLACE?");
URLConnection suir_connection = suir_url.openConnection();
suir_connection.setDoOutput(true);
PrintWriter suir_out = new
PrintWriter(suir_connection.getOutputStream());
suir_out.println(MyDataString +"#DREENDDATA" +"\n\n"); // PASS data
as POST To Suir
suir_out.close();


Regards.
 
S

Scott Allen

There are two classes you can use: WebRequest and WebClient. WebClient
is the simplest API, but doesn't do everything WebRequest
(HttpWebRequest) can do.

using(WebClient webClient = new WebClient())
{
byte[] response = webClient.DownloadData("http://foo.com");
Response.OutputStream.Write(response, 0, response.Length);
}
 
K

K R

Hi Scott,

Thanks for your response. But this is only half part of my question.
After creating connection I have to pass another string value to server
using POST method. Appreciate if you help me out on this.

Regards,
Kiran
 
G

Girish Bharadwaj

Umm.. WebRequest has methods to set the ContentType,ContentLength and a
RequestStream that you can write to.

Or with WebClient, just OpenWrite() to get a Stream and write the string
into that Stream. After that do a OpenRead() and read the data out..
 
S

Scott Allen

Sorry Kiran - I wasn't sure what the PrintWriter was doing. As Girish
mentioned there is a RequestStream you can write into with a
WebRequest class. I have an example of using this in an article:

Screen Scraping, ViewState, and Authentication using ASP.Net
http://odetocode.com/Articles/162.aspx

Using the simpler WebClient would take a couple more lines of code:

webClient.Headers.Add("Content-Type",
"application/x-www-form-urlencoded");
webClient.UploadData( "<theUrl>", "POST",
Encoding.ASCII.GetBytes("<POSTstring">)

HTH,
 

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

Members online

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top