开发者

equal of php in asp.net

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);
e开发者_JS百科cho $response;
?>

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


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)


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜