RestSharp Windows Phone 7 错误请求

发布于 2024-12-08 06:20:16 字数 3604 浏览 0 评论 0原文

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

<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 库使用该服务,方法是使用以下代码:

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:// 使用 SOAP 端点liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/soap/SaveNewSightingRest,我刚刚得到一个“坏请求”状态码。

为什么会出现这种情况?有什么办法可以让它发挥作用吗?如果它有任何帮助的话,每次我单击为 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>

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.

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);
    }
}

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.

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.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-12-15 06:20:16

所以我不必发表评论:

What does your body Look like in fiddler, with RestSharp you can create a poco and simple, 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/resource,在 fiddler 中,正文为: 为了澄清,这是正在发送的正文,因此您希望看到您的 json正在尝试发送。

有错误:

服务器在处理请求时遇到错误。例外情况
消息是“无法反序列化根名称为“String”的 XML 正文,并且
根命名空间 '' (用于操作 'SaveNewSightingRest' 和合约
('ISightingServiceRest', 'http://tempuri.org/')) 使用
数据契约序列化器。确保XML的类型对应
添加到服务的已知类型集合中。

So I don't have to post in comments:

What does your body look like in fiddler, with RestSharp you can create a poco and simply, RestRequest.AddBody(MyPoco);

I think the url:

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

should be:

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

and then:

RestRequest.Resource = "json/SaveNewSightingRest";

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.

with error:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文