RestSharp Windows Phone 7 错误请求
我一直在尝试制作一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我不必发表评论:
What does your body Look like in fiddler, with RestSharp you can create a poco and simple,
RestRequest.AddBody(MyPoco);
我认为网址:
应该是:
然后:
更新:
我复制了您的代码,更改了 url/resource,在 fiddler 中,正文为:
为了澄清,这是正在发送的正文,因此您希望看到您的 json正在尝试发送。有错误:
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:
should be:
and then:
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: