Code source d’intégration C Sharp
Après avoir défini le code source, insérez-le.
Nous sommes maintenant prêts à insérer le code. Double-cliquez sur le bouton Envoyer un service Web sur votre formulaire pour ouvrir le code d’arrière-plan du formulaire qui a été créé. Voici le code pour insérer un enregistrement dans l’instance de démonstration et lire la réponse.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ExampleWebServiceForWiki
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
/* SERVICE REFERENCE-SPECIFIC CODE
ServiceReference1.ServiceNowSoapClient soapClient = new ServiceReference1.ServiceNowSoapClient();
soapClient.ClientCredentials.UserName.UserName ="itil";
soapClient.ClientCredentials.UserName.Password ="itil";
ServiceReference1.insert insert = new ExampleWebServiceForWiki.ServiceReference1.insert();
ServiceReference1.insertResponse response = new ExampleWebServiceForWiki.ServiceReference1.insertResponse();
// END OF SERVICE REFERENCE CODE */
// WEB REFERENCE-SPECIFIC CODE
WebReference1.ServiceNow_incident soapClient = new ExampleWebServiceForWiki.WebReference1.ServiceNow_incident();
System.Net.ICredentials cred = new System.Net.NetworkCredential("itil", "itil");
soapClient.Credentials = cred;
WebReference1.insert insert = new WebReference1.insert();
WebReference1.insertResponse response = new WebReference1.insertResponse();
// END OF WEB REFERENCE CODE
insert.category = "Category";
insert.comments = "Comments";
insert.short_description = "My short description";
try
{
response = soapClient.insert(insert);
this.richTextBoxResult.Text = "Incident Number: " + response.number + "\n";
this.richTextBoxResult.Text += "Sys_id: " + response.sys_id;
}
catch (Exception error)
{
this.richTextBoxResult.Text = error.Message;
}
}
}
}