CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Aug 2005
    Posts
    4

    Posting to URL using PHP

    Hi,

    I wish to send some XML (preformatted into a string) to a URL using PHP.

    I have the following code to illustrate my requirement, but having spent last hour googling I can't find anything suitable in php. Any ideas? Is it possible? Thanks :-)

    Code:
    private static string DoPost(string url,string data)
      {
       
       bool got_exception=false;
       data="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + data;
       String result = "";
       StreamWriter myWriter = null;
       HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
       Rpc.Rpc.SetWebProxy(objRequest);
       objRequest.Timeout=60000;
       objRequest.Method = "POST";
       objRequest.Credentials = CredentialCache.DefaultCredentials;
       objRequest.ContentType = "text/xml";
       try
       {
        myWriter = new StreamWriter(objRequest.GetRequestStream());
        myWriter.Write(data);
       }
       catch(System.Net.WebException we)
       { got_exception=true;
        if(we.Status!=WebExceptionStatus.Timeout)
         throw we;
       }
       catch (Exception e) 
       {
        throw e;
       }
       finally 
       {
        try
        {
         myWriter.Close();
        }
        catch(Exception)
        {}
       }
       if(got_exception) return "";
       try
       {
        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
        {
         result = sr.ReadToEnd();
     
         // Close and clean up the StreamReader
         sr.Close();
        }
    Last edited by Ravanol; August 12th, 2005 at 12:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured