1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace WindowsFormsApplication1
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//ReponseXML(url, xmlText,method);
string url2 = "http://1.8.2.1:80/ISAPI/System/IO/outputs/1/trigger";
string method2 = "put";
string xmlText2= File.ReadAllText(@"file1.xml");
ReponseXML(url2, xmlText2, method2);
Application.Run(new Form1());
}
public static string ReponseXML(string destinationUrl, string requestXml, string method)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
request.Credentials = new NetworkCredential("admin", "1234");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentType = "text/xml; encoding='utf-8'";
request.Accept = "text/xml";
request.ContentLength = bytes.Length;
if (method == "get")
{
request.Method = "GET";
}
else
if (method == "post")
{
request.Method = "POST";
}
else
if (method == "delete")
{
request.Method = "DELETE";
}
else
if (method == "put")
{
request.Method = "PUT";
}
else
{
throw new Exception("Invalid Method Type");
}
request.Timeout = -1;
//request.AuthenticationLevel = 0;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
MessageBox.Show(responseStr);
return responseStr;
}
else
{
MessageBox.Show("statut ko");
}
requestStream.Close();
}
catch (WebException e)
{
throw e;
}
return null;
}
}
} |
Partager