// This function converts a string representation // of hexadecimal bytes into a byte array public static byte[] ConvertHexToBytes(string hexString) { int numChars = hexString.Length; byte[] byteArray = null; if (numChars > 0) { try { byteArray = new byte[numChars / 2]; for (int i = 0; i < numChars; i += 2) { byteArray[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16); } } catch (Exception ex) { ASError.ReportException(ex); return(null); } } return(byteArray); }
// This function sends the OPTIONS request and returns an // ASOptionsResponse class that represents the response. public ASOptionsResponse GetOptions() { if (credential == null || server == null) { throw new InvalidDataException("ASOptionsRequest not initialized."); } string uriString = string.Format("{0}//{1}/Microsoft-Server-ActiveSync", UseSSL ? "https:" : "http:", Server); Uri serverUri = new Uri(uriString); CredentialCache creds = new CredentialCache(); // Using Basic authentication creds.Add(serverUri, "Basic", Credentials); HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(uriString); httpReq.Credentials = creds; httpReq.Method = "OPTIONS"; try { HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse(); ASOptionsResponse response = new ASOptionsResponse(httpResp); httpResp.Close(); return(response); } catch (Exception ex) { ASError.ReportException(ex); return(null); } }
// This function sends the request and returns // the response. public ASCommandResponse GetResponse() { GenerateXMLPayload(); if (Credentials == null || Server == null || ProtocolVersion == null || WbxmlBytes == null) { throw new InvalidDataException("ASCommandRequest not initialized."); } // Generate the URI for the request string uriString = string.Format("{0}//{1}/Microsoft-Server-ActiveSync?{2}", useSSL ? "https:" : "http:", server, RequestLine); Uri serverUri = new Uri(uriString); CredentialCache creds = new CredentialCache(); // Using Basic authentication creds.Add(serverUri, "Basic", credential); HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(uriString); httpReq.Credentials = creds; httpReq.Method = "POST"; // MS-ASHTTP section 2.2.1.1.2.2 httpReq.ContentType = "application/vnd.ms-sync.wbxml"; if (!UseEncodedRequestLine) { // Encoded request lines include the protocol version // and policy key in the request line. // Non-encoded request lines require that those // values be passed as headers. httpReq.Headers.Add("MS-ASProtocolVersion", ProtocolVersion); httpReq.Headers.Add("X-MS-PolicyKey", PolicyKey.ToString()); } try { Stream requestStream = httpReq.GetRequestStream(); requestStream.Write(WbxmlBytes, 0, WbxmlBytes.Length); requestStream.Close(); HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse(); ASCommandResponse response = WrapHttpResponse(httpResp); httpResp.Close(); return(response); } catch (Exception ex) { ASError.ReportException(ex); return(null); } }
// This function moves local storage to a new directory. public void MoveLocalFolder(string destination) { try { Directory.Move(saveLocation, destination); } catch (Exception e) { ASError.ReportException(e); } saveLocation = destination; }
// This function uses the ASWBXML class to encode // XML into a WBXML stream. private byte[] EncodeXMLString(string xmlString) { try { ASWBXML encoder = new ASWBXML(); encoder.LoadXml(xmlString); return(encoder.GetBytes()); } catch (Exception ex) { ASError.ReportException(ex); return(null); } }
// This function uses the ASWBXML class to decode // a WBXML stream into XML. private string DecodeWBXML(byte[] wbxml) { try { ASWBXML decoder = new ASWBXML(); decoder.LoadBytes(wbxml); return(decoder.GetXml()); } catch (Exception ex) { ASError.ReportException(ex); return(""); } }