P
pintu
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..
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..