我一直在试图制作一个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服务的链接,我只是得到一个空白屏幕。
感谢您的帮助!
看来问题就出在服务本身,因为我尝试通过URL请求访问端点,但它抛出一个400错误请求错误。 :( –
我现在就做,并将其添加到原始问题编辑:) –