错误的请求 RestSharp Windows Phone 7 [英] Bad Request RestSharp Windows Phone 7

查看:61
本文介绍了错误的请求 RestSharp Windows Phone 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作一个 WCF Web 服务,它可以将一组特定的数据输入到数据库中,并且可以在 Windows Phone 7 中使用.在配置文件中,有两个端点:json 和 soap.这是他们两个的代码:

I've been trying to make a WCF web service that enters a certain set of data into a database and it can be used in Windows Phone 7. In the configuration file there are two endpoints: json and soap. Here's the code for both of them:

<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 端点生成配置.

Now, I deployed the service to a server, downloaded its metadata with slsvcutil.exe and stored both the code file and the ServiceReferenceConfig file in the project. Now, the slsvcutil only generates the configuration for the soap endpoint.

现在,我尝试使用 RestSharp 库使用该服务,方法如下:

Now, I try to use the service using the RestSharp library, by using the following code:

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,我会收到找不到端点"错误,这是意料之中的.但是,如果我尝试通过 URL http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/soap/SaveNewSightingRest,我只收到错误请求"状态代码.

If I try to use the URL given in the sample code, I get an "endpoint not found" error, which is to be expected. However, if I try to use the SOAP endpoint via the URL http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/soap/SaveNewSightingRest, I just get a "Bad Request" status code.

为什么会这样?有什么办法可以让它工作吗?如果有帮助的话,每次我点击我为 SOAP 服务提供的链接时,我都会看到一个空白屏幕.

Why does this happen? Is there any way I can make it work? If it can be any helpful, everytime I click on the link I provided for the SOAP service I just get a blank screen.

感谢您的帮助!

推荐答案

所以我不必在评论中发帖:

So I don't have to post in comments:

你的身体在 fiddler 中是什么样子的,使用 RestSharp 你可以创建一个 poco 并且很简单,RestRequest.AddBody(MyPoco);

What does your body look like in fiddler, with RestSharp you can create a poco and simply, 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";

更新:我复制了你的代码,修改了 url/资源,在 fiddler 中,正文是: <String/> 澄清一下,这是发送的正文,所以你希望看到你的 json正在尝试发送.

update: I copied your code, chanding the url/resource and in fiddler the body is: <String /> To clarify, this is the body being sent, so you'd expect to see the json you're trying to send.

有错误:

服务器在处理请求时遇到错误.例外消息是无法反序列化根名称为字符串"的 XML 主体,并且根命名空间 ''(用于操作 'SaveNewSightingRest' 和合同('ISightingServiceRest', 'http://tempuri.org/')) 使用DataContractSerializer.确保 XML 对应的类型添加到服务的已知类型集合中.

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.

这篇关于错误的请求 RestSharp Windows Phone 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆