System-to-System Developer’s Guide v3.0
Confidential Material 19 of 19
APPENDIX C – MICROSOFT .NET C# CODE SAMPLE
Following is a Microsoft .NET C# code sample taken from the NetSuite smbXMLPost tool that illustrates how a two-way
SSL connection is established in the .NET platform.
// NetSuite URL to post to
string url =
"https://partners.netsuite.com/s/SmbXml?pid=22999&pacct=ShutterFly";
HttpWebRequest req;
HttpWebResponse res;
// Create request object that connects to NetSuite
req = (HttpWebRequest) WebRequest.Create( url );
req.Method = "POST";
req.ContentType = "text/xml";
// The path to the certificate.
string certFilePath = "C:\\client.der";
// Load the certificate into an X509Certificate object.
X509Certificate x509Cert = X509Certificate.CreateFromCertFile( @certFilePath );
// Add certificate to request
req.ClientCertificates.Add( x509Cert );
// Read smbXML file and write contents to request stream
StreamReader inFileReader = new StreamReader("c:\\queryAllSalesOrders.xml");
StreamWriter reqWriter = new StreamWriter( req.GetRequestStream() );
string sLine = null;
while ( ( sLine = inFileReader.ReadLine() ) != null )
{
reqWriter.WriteLine( sLine );
}
inFileReader.Close();
reqWriter.Close();
// Get response
res = (HttpWebResponse) req.GetResponse();
System.Console.Out.WriteLine( "Status Code: " + res.StatusCode );
StreamReader responseReader =
new StreamReader(res.GetResponseStream(), Encoding.ASCII);
// Put the results in a string and print to console
System.Console.Out.WriteLine("\n");
strResult = responseReader.ReadToEnd();
responseReader.Close();
Console.WriteLine(strResult);