I've this WCF web service hosted inside a Sharepoint 2010 web application.
This service is created using the following factory:
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
I can successfully access this service from a Visual Web Part inside the same SP web application via jQuery.
Now I need to access it from a completely independant C# project outside Sharepoint.
Using the following code returns a 400 - Bad request
// local
string uri = @"http://sharepoint-site-domain/_vti_bin/namespace/serviceName.svc/serviceMethod";
// create the web request object
var request = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
request.Method = "Get"; // Post method
request.ContentType = "application/json; charset=utf-8";
// Send the data to the webserver
var response = request.GetResponse(); // throws 400 - Bad request exception
// create a stream reader
using (var reader = new StreamReader(response.GetResponseStream()))
{
// get the response text
responseText = reader.ReadToEnd();
}
Also take in consideration that this application will receive the Web method URI as a parameter and call the method, so adding the reference to a specific web service instance in the C# project is not an option.
How can I access it?
Thanks
UPDATE
Here are the headers copied from Fiddler when the REST service is called in the Sharepoint site via jQuery:
GET http://spsiteurl/_vti_bin/serviceNamespace/Service.svc/Method?_=1355849073206 HTTP/1.1
Host: spsiteurl
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: spsiteurl/Pages/SomePage.aspx
Cookie: OfflineClientInstalled=0; http://spsiteurl=PrevWorkbookUrl=J082NAOxhS+hD3YntFcGpENzasyP3LUFrNLcxX7/DsWKoqUxbUXkz8RlcMew6hXOhcxSGRzQiLljOduVZtpt36KhVhYZXWF1m27PgamZZp9F3owZ3O0YMw41m+IGWA78jilTnqbiz+TrJpEbmfqYinMLbpMvw0zxaZOIjD3iyLQgVkqZLIOcUAAgPjLjjGPPrBPeUxrazoLVcWxFduLeRl6pGXb2yfC6M9N49CH7nhEZqm03d8ebaiOJII3jRYaKORk3UzjPmXQ=&ClientPermissions=J082NAOxhS+hD3YntFcGpENzasyP3LUFrNLcxX7/DsWKoqUxbUXkz8RlcMew6hXOhcxSGRzQiLljOduVZtpt30+39ImUYscvwSdMGCDgdHb/85CJpVrvCJ657VMStRjuEGqIeCo9z++Au/7/1uzt3ib4rtgrkYyEMjFVXfxHYNVRlWuTCRH0wu8j+lKTh2zw&FirstColumn=0&FirstRow=0&ActiveSheetName=Budget%20Summary&EWRLT=07/31/2012 19:30:55&cwut=http%3A%2F%2F374848%2Dweb1%3A23170%2FSynxi%20Demo%20Data%2FFinance%5FBudget%5F2013%2Exlsx&wfnt=Finance%5FBudget%5F2013%2Exlsx&SessionId=36.a71dab4d-da9e-4729-850f-ee87e92664ed162.1.V21.1i/9tAJ4bohdi2eqj0yR990.5.en-US5.en-US73.+0300#0000-11-00-01T02:00:00:0000#+0000#0000-03-00-02T02:00:00:0000#-006036.b4b95b83-1e5e-436e-b5c4-20a8429ffe331.N; TimerJobsPage=CurrentMode=WebApplication; Microsoft.SharePoint.Administration.SPServer=CurrentId=65cad12d-0358-4b7b-b0fb-66ff13df87c9; Microsoft.SharePoint.Administration.SPWebApplication=CurrentId=b89d78b2063d4ac4810ebde4e04a829e; digInstalled=0; stsSyncAppName=Client; stsSyncIconPath=; databaseBtnText=0; databaseBtnDesc=0; loginAsDifferentAttemptCount=; previousLoggedInAs=; WSS_KeepSessionAuthenticated={b89d78b2-063d-4ac4-810e-bde4e04a829e}; http://374848-web1:23429=PrevWorkbookUrl=ISCYczELEmZ3gKRBAW0D247X81zt/BfamCjgrIgoKzQDAfe6ndSvRRfLtNodMlAV4SyQcg3m1wzMjOLoZusllnD6MTYotG6tbAzjz02XyRxTEuKyWLMFsAO9gMC0qO/qUfCCLpF8u2Xd5Bkk/u+qO/JRPg5BWQuNl696GfvxCZfGMKvwgsAZ1bsCCJLFRS0hXzig0BJYBYuhCjYF2+ODEN1stZLxbd5eQpSwJzZajuKndO8O/0EhmfgORWf2CWmY42gDXCPiAwg=&ClientPermissions=ISCYczELEmZ3gKRBAW0D247X81zt/BfamCjgrIgoKzQDAfe6ndSvRRfLtNodMlAV4SyQcg3m1wzMjOLoZuslls88H/HhVfg4hpoUkHwpUb/knahen7/IFvYhv53qkfF5mX0Ht4/VyuQgrnntFMWnIP4UNsuPMvXcivx5kgpKNVZgFfjK/TkL1WrCNggTvYPB&FirstColumn=0&FirstRow=0&ActiveSheetName=Budget%20Summary&EWRLT=11/30/2012 21:58:33&cwut=http%3A%2F%2F374848%2Dweb1%3A23429%2FShared%20Documents%2FFinance%5FBudget%5F2013%2Exlsx&wfnt=Finance%5FBudget%5F2013%2Exlsx&SessionId=36.a71dab4d-da9e-4729-850f-ee87e92664ed162.1.V21.1fkgT6S2xnz8Yv6QpyHV090.5.en-US5.en-US73.+0300#0000-11-00-01T02:00:00:0000#+0000#0000-03-00-02T02:00:00:0000#-006036.b684c406-404f-4aae-ade7-e18d8ce9d01e1.N; Ribbon.WebApp=1440531|-1|625|-1509927122
So there are a lot of Cookies embedded in the request I think. How can I replicate them when calling it from outside in C#?
The service contract:
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Test();
Implementation:
public string Test()
{
return "You are in Site Collection: " + SPContext.Current.Web.Title;
}