1

what is the equivalent code in asp.net language???

<?php
$ch = curl_init("http://irnafiarco.com/queue");
$request["queue"] = file_get_contents("/path_to_my_xml_file/my_xml_file.xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);
echo $response;
?>

in http://irnafiarco.com/queue a Listener that get requst xml file and saver xml file.

4
  • So, you given us homework!!!!! Commented Oct 10, 2011 at 8:22
  • Could you please explain what the code does for us ASP.NET developers. Commented Oct 10, 2011 at 8:23
  • @ sikender.no this is a real question Commented Oct 10, 2011 at 8:31
  • 1
    (related) first google serp for "asp.net curl" => forums.asp.net/t/788908.aspx/1 Commented Oct 10, 2011 at 8:36

2 Answers 2

1

Using WebRequest, this will be the basic code

   var req = WebRequest.Create(@"http://irnafiarco.com/queue"))

   // prepare the request
   req.ContentType = "application/x-www-form-urlencoded";
   req.Method = "POST";

   // push the file contents into request body
   var data = "queue=" + System.IO.File.OpenText(filePath).ReadToEnd();
   var bytes = System.Text.Encoding.Ascii.GetBytes(data );  
    request.ContentLength = bytes.Length;
   var rs = req.GetRequestStream();
   rs.Write(bytes, 0, bytes.Length); 
   rs.Close ();

   // get the response
   var resp = req.GetResponse();
   var sr = new System.IO.StreamReader(resp.GetResponseStream());
   var result = sr.ReadToEnd();

Disclaimer: untested code

EDIT:
Added the post parameter name ("queue") which I have missed in first draft. Also added content-length for the request. This code should get you started. The basic idea is you need to simulate exact post request generated by PHP code. Use tool such as Fiddler/ Firebug on FF to inspect & compare request/response from PHP and .NET code.

Further, I suspect that the PHP code may generating request with content type as multipart/form-data. However, I believe that server should also able to support the post body with application/x-www-form-urlencoded (because we have only one parameter in body) but in case it doesn't work and you must generate POST body as multipart/form-data then it will be little more involved. See this SO question where accepted answer has given the sample code for the same : Upload files with HTTPWebrequest (multipart/form-data)

Sign up to request clarification or add additional context in comments.

1 Comment

@GodIsLive, corrected the code and also see my edit in the answer.
1

Take a look at WebRequest, WebProxy classes which are inline with what you're after...

WebRequest request = WebRequest.Create(url);
request.Proxy = new WebProxy("http://blahblahblah", true)
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// handle response here

Also, see here, and here, though may not be relevant for your implementation

Examples of using these to fetch XML abound, i.e.:

System.Net.HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create("yourURL.xml");
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Accept = "text/xml";
System.Net.HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
System.IO.Stream responseStream = webResponse.GetResponseStream();
System.Xml.XmlTextReader reader = new XmlTextReader(responseStream);
//Do something meaningful with the reader here
reader.Close();
webResponse.Close();

1 Comment

if response have a xml file how handle this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.