using System;
using System.IO;
using System.Text;

public static void Main()
{
	//-- Intent: updates the given contact on the contact list with listcode of 123456 --//
	//-- PUT to the following url --//
	string my_url = "http://api.v4.swiftreach.com/api/ContactLists/Contacts/Update/123456/736c8340-e23b-4c23-aaa1-f55cfdb391bb";
	//-- our API KEY credentials --//
	string API_KEY = "12345";

	UriBuilder my_uri = new UriBuilder(my_url);
	my_uri.Port = 80;
	System.Net.HttpWebRequest MyRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(my_uri.Uri);
	//-- Update Email is a PUT --//
	MyRequest.Method = "PUT";
	//-- Append the API KEY header --//
	MyRequest.Headers.Add("SwiftAPI-Key", API_KEY);

	//-- build the Email_Message XML to POST --//
	string create_email = "<Contact xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema/SwiftReach.Swift911.Core.ContactLists\">"
				+"<Addresses>"
				+"<AddressProfile>"
				+"<Address1>14 Industrial Ave</Address1>"
				+"<Address2></Address2>"
				+"<AddressGuid>22748f25-07b8-459c-bcef-ce2ba851a84c</AddressGuid>"
				+"<City>Mahwah</City>"
				+"<County></County>"
				+"<IsPrimary>false</IsPrimary>"
				+"<Label>Office</Label>"
				+"<Latitude>41.052698269486427</Latitude>"
				+"<Longitude>-74.121656566858292</Longitude>"
				+"<PostalCode>07430</PostalCode>"
				+"<State>NJ</State>"
				+"</AddressProfile>"
				+"</Addresses>"
				+"<Email>info@swiftreach.com</Email>"
				+"<EntityGuid>736c8340-e23b-4c23-aaa1-f55cfdb391bb</EntityGuid>"
				+"<EntityName>Bob</EntityName>"
				+"<Fax>"
				+"</Fax>"
				+"<Pager></Pager>"
				+"<PagerNetwork>0</PagerNetwork>"
				+"<Phones>"
				+"<PhoneProfile>"
				+"<AnsDetectionOverride>UseDefault</AnsDetectionOverride>"
				+"<Extension></Extension>"
				+"<OptInSMS>true</OptInSMS>"
				+"<Phone>2012361344</Phone>"
				+"<PhoneLabel>Mobile</PhoneLabel>"
				+"<PhoneType>phonetype_voice</PhoneType>"
				+"</PhoneProfile>"
				+"</Phones>"
				+"<PinCode></PinCode>"
				+"<SMSNetwork>0</SMSNetwork>"
				+"<SMSPhone></SMSPhone>"
				+"<SpokenLanguage>English</SpokenLanguage>"
				+"<Tag></Tag>"
				+"<UserDefined />"
				+"</Contact>";

	//-- PUt the xml --//
	byte[] bytes = System.Text.Encoding.ASCII.GetBytes(create_email);
    MyRequest.ContentLength = bytes.Length;
    MyRequest.ContentType = "text/xml; encoding='utf-8'";
    Stream s = MyRequest.GetRequestStream();
    s.Write(bytes, 0, bytes.Length);
    s.Close();

	try
    {
        //-- get the response --//
		System.Net.HttpWebResponse MyResponse = (System.Net.HttpWebResponse)MyRequest.GetResponse();
        string message = String.Format("Received HTTP {0}", MyResponse.StatusCode);
        using (StreamReader sr = new StreamReader(MyResponse.GetResponseStream()))
        {
            //-- Create was successful --//
			string response_body = sr.ReadToEnd();
			System.Console.WriteLine("SUCCESS - " + response_body);
        }
    }
    catch (System.Net.WebException webEx)
    {        
        //-- Create failed --//
		using (StreamReader sr = new StreamReader(webEx.Response.GetResponseStream()))
        {
            string response_body = sr.ReadToEnd();
            string error_message = String.Format("Received HTTP {0} : {1}", webEx.Status.ToString(), response_body);
			System.Console.WriteLine("ERROR - " + error_message);
        }
    }
}