2011-10-04 67 views
2

我一直在试图制作一个WCF Web服务,它将一组数据输入到数据库中,并且可以在Windows Phone 7中使用它。在配置文件中有两个端点:json和肥皂。下面是两个人的代码:Bad Request RestSharp Windows Phone 7

<service name="LiveAndesWCF.SightingServiceRest" behaviorConfiguration="MetadataBehavior"> 
    <endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.ISightingServiceRest"/> 
    <endpoint address="json" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="LiveAndesWCF.ISightingServiceRest"/> 
</service> 
... 
<bindings> 
    <basicHttpBinding> 
     <binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"> 
      <readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/> 
     </binding> 
    </basicHttpBinding> 
     <webHttpBinding> 
      <binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"> 
       <readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/> 
      </binding> 
     </webHttpBinding> 
</bindings> 

现在,我部署的服务的服务器,下载与slsvcutil.exe和存储两个密码文件,并在项目中ServiceReferenceConfig文件元数据。现在,slsvcutil只为soap端点生成配置。

现在,我尝试使用RestSharp库使用该服务,通过使用下面的代码:如果我尝试使用示例代码给出的URL

public static void sendSighting() 
{ 
    string URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/json/SaveNewSightingRest"; 

    var request = new RestRequest(Method.POST); 

    var client = new RestClient(URL); 

    request.AddHeader("Accept", "application/json"); 
    request.AddHeader("Host", "liveandesor.web711.discountasp.net"); 
    request.AddHeader("Content-type", "application/json"); 

    JObject json = new JObject(); 

    json.Add("UserId", "letroll"); 
    json.Add("Location", "TEST"); 
    json.Add("Longitude", 50); 
    json.Add("Latitude", 50); 
    json.Add("Comment", "TEST"); 
    json.Add("SpeciesId", 438); 
    json.Add("_Date", new DateTime(2011, 8, 12)); 
    json.Add("DateString", (new DateTime(2011, 8, 12)).ToString()); 
    json.Add("SightingTypeId", 1); 
    json.Add("Quantity", 1); 
    json.Add("AgeId", 1); 
    json.Add("SexId", 1); 
    json.Add("PhotoURL", new JArray(new List<String>())); 
    json.Add("Request", false); 
    json.Add("Visibility", false); 
    json.Add("SightingStateId", 1); 
    json.Add("Created_at", new DateTime()); 
    json.Add("Updated_at", new DateTime()); 

    String jason = json.ToString(); 

    try 
    { 
     //request.AddObject(json); 
     request.AddBody(jason); 

     RestResponse resource; 

     client.ExecuteAsync(request, (response) => 
     { 
      resource = response; 
      string content = resource.Content; 
      string status_code = resource.StatusCode.ToString(); 
      string response_status = resource.ResponseStatus.ToString(); 
     }); 
    } 
    catch (Exception e) 
    { 
     string error = "Error: " + e.ToString() + "\n. Stack Trace: " + e.StackTrace; 
     Console.WriteLine(error); 
    } 
} 

,我得到没有找到一个“端点“错误,这是可以预料的。但是,如果我尝试通过URL http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/soap/SaveNewSightingRest使用SOAP端点,我只会收到“错误的请求”状态码。

为什么会发生这种情况?有什么办法可以让它工作吗?如果它可以是任何有用的,每次我点击我提供的SOAP服务的链接,我只是得到一个空白屏幕。

感谢您的帮助!

+0

看来问题就出在服务本身,因为我尝试通过URL请求访问端点,但它抛出一个400错误请求错误。 :( –

+0

我现在就做,并将其添加到原始问题编辑:) –

回答

2

所以我没有在意见发布:

什么是你的身体看起来就像小提琴手,与RestSharp您可以创建一个POCO和简单,RestRequest.AddBody(MyPoco);

我觉得网址:

string URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/json/SaveNewSightingRest"; 

应该是:

string URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc"; 

,然后:

RestRequest.Resource = "json/SaveNewSightingRest"; 

更新: 我复制你的代码,chanding网址/资源和提琴手身体:<String />为了澄清,这是正在发送的身体,所以你希望看到你的JSON试图发送。

错误:

The server encountered an error processing the request. The exception message is 'Unable to deserialize XML body with root name 'String' and root namespace '' (for operation 'SaveNewSightingRest' and contract ('ISightingServiceRest', 'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

+0

德里克:这正是我需要的!我将信息发送到服务器的方式出错,特别是使用空字符串数组。无论如何,我无法告诉你你有多大的帮助。我一整天都在寻找答案。非常感谢!! :d –