sending data to another server and receiving data as response

Discussion in 'ASP .Net Web Services' started by pintu, Dec 14, 2006.

  1. pintu

    pintu Guest

    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..
     
    pintu, Dec 14, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    498
  2. Jan Li?e
    Replies:
    6
    Views:
    767
    Steve Horsley
    Feb 8, 2004
  3. TreatmentPlant
    Replies:
    4
    Views:
    413
    Default User
    Oct 17, 2007
  4. subeen
    Replies:
    2
    Views:
    591
    subeen
    Oct 4, 2009
  5. Mike

    Receiving and sending data

    Mike, Aug 2, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    122
Loading...

Share This Page