Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exchange Web Services - The response received from the service didn't contain valid XML

I am attempting to connect to exchange web services (ews) on a exchange 2010 server. Here is the code I am using:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Exchange.WebServices.Data;

namespace NDR_Processor
{
    class Program
{
    static void Main(string[] args)
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        service.Credentials = new System.Net.NetworkCredential("redacted", "redacted", "redacted");

        service.Url = new Uri("https://exchange.redacted.net/EWS/Exchange.asmx");

        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000));

        foreach (Item item in findResults.Items)
        {
            Console.WriteLine(item.Subject);
            Console.WriteLine(item.Body);

        }
    }
}
}

However in doing so I get an error stating "The response received from the service didn't contain valid XML.". The inner exception indicates: {"Data at the root level is invalid. Line 1, position 1."}

I've tried hitting https://exchange.redacted.net/EWS/Exchange.asmx in a web browser, it prompts me to login and then I am presented with a valid XML document as far as I can tell. So I am at a loss as to why my application is choking.

Does anyone have any ideas for why this might be happening or how I can solve it?

Thanks Brad

like image 360
Brad Avatar asked Jul 05 '12 12:07

Brad


2 Answers

service.Url = new Uri("https://mail.tencent.com/EWS/Exchange.asmx");

Details info is here: c# programmatically reading emails from the Exchange server

like image 54
dabuwang Avatar answered Sep 23 '22 07:09

dabuwang


I was experiencing the same issue as described in the following forum post: http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/e54c217f-28ff-4626-8ce8-a1242081f4d1/

(Essentially extra characters were being pre-pended and appended to the xml returned causing the error above)

If its any help - deleting and re-creating EWS virtual directory did not alleviate the problem.

I believe that perhaps our F5 load balancer or some intermediary device is inserting extra characters at the beginning or end of the XML.

When I changed my code to: service.Url = new Uri("https://192.168.x.x/EWS/Exchange.asmx");

(Essentially using the internal IP address of our exchange server) the request worked just fine. So something outside of exchange is mangling the XML.

like image 28
Brad Avatar answered Sep 19 '22 07:09

Brad