Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > sending data to another server and receiving data as response

Reply
Thread Tools

sending data to another server and receiving data as response

 
 
pintu
Guest
Posts: n/a
 
      12-14-2006
Hi..I posted my message earlier but it was not properly described..so
am posting again.

I am working in an application in which i hav to send the contents of
an xml file(from my local machine) to another server(www.ups.com) as
http-request.As response i will get another xml file from that server.
So what i did is i had
1.collected the data by using webrequest and webresponse class's
methods (like webreq.create,webreq.getresponse and
webres.getresponsestream methods) and used http-get
2.posted the data to that server by creating another webrequest and
webresponse. then called the webreq.getrequeststream() .
Upto this there is no build error..Is it the right way for doing that ?
If so then my second step is to get the response from that server in
xml and then overwrite my original file with that xml.I dont hav any
idea about how to do this..I tried it in the following way,but not sure
whether it will work or not.I am writing the entire code below..Plz
help me asap to solve the proble..Thanks in advance.

public bool UploadFile(string filename)
{
try
{
// GET XML - read XML, get it into string
WebRequest webreq;
WebResponse webres;
webreq = WebRequest.Create("D://UPS/"+filename);
webres = webreq.GetResponse();
StreamReader sReader = new
StreamReader(webres.GetResponseStream());
string strData = sReader.ReadToEnd();
webres.Close();

//Post data
string querystring = strData.ToString();
WebRequest webReqForPost =
WebRequest.Create("http://localhost/test/Testupload/");
webReqForPost.ContentType = "multipart/form-data";
webReqForPost.Method = "POST";
Stream strNewStream = webReqForPost.GetRequestStream();
byte[] temp_byteArray;
temp_byteArray = (new
UnicodeEncoding()).GetBytes(querystring);
strNewStream.Write(temp_byteArray, 0,
temp_byteArray.Length);
strNewStream.Close();

//get data from url connection and return the xml document
as string
string data = "";
try
{
WebRequest wr =
WebRequest.Create("http://localhost/test/Testupload/");
wr.Method = "HEAD";
WebResponse ws = wr.GetResponse();
data = readURLConnection(ws);
OverwriteFile(data,filename);
}
catch(Exception ex)
{
throw ex;
}
return true;
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
return false;
}

}
private static String readURLConnection(WebResponse ws)
{
StringBuilder buffer = new StringBuilder();
StreamReader rdr = null;
try
{
//rdr = new StreamReader(new
StreamReader(ws.GetResponse().GetResponseStream(),
Encoding.Default).BaseStream, new
StreamReader(ws.GetResponse().GetResponseStream(), Encoding.Default).CurrentEncoding);
rdr = new StreamReader(new
StreamReader(ws.GetResponseStream(), Encoding.Default).BaseStream, new
StreamReader(ws.GetResponseStream(),
Encoding.Default).CurrentEncoding);
int letter = 0;
while ((letter = rdr.Read()) != -1)
buffer.Append((char)letter);
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error in Getting
Response");
throw ex;
}
finally
{
rdr.Close();
}
return buffer.ToString();
}

private static void OverwriteFile(string data, string fname)
{
try
{
System.Net.WebClient client = new System.Net.WebClient();
client.Credentials = new NetworkCredential();
string filepath = @"D:\UPS\" + fname;
byte[] byteArray;
byteArray = (new UnicodeEncoding()).GetBytes(data);
client.UploadData(filepath, "PUT", byteArray);
}
catch
{
HttpContext.Current.Response.Write("Error Overwriting
File");
}

}

Thanks again..

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble sending / receiving compressed data (using zlib) as HTTP POSTto server (in django) subeen Python 2 10-04-2009 06:59 PM
Sending and Receiving HTTP data.... newbie TreatmentPlant C++ 4 10-17-2007 05:39 PM
Should my PC be sending and receiving data onto/from the internet on its own? admyc Computer Security 1 04-26-2007 09:26 PM
Receiving and sending data Mike ASP .Net Web Services 0 08-02-2006 09:07 PM
Sending and receiving data over network Jan Li?e Java 6 02-08-2004 11:55 AM



Advertisments