RESTful WCF サービスにアクセスしようとしている Windows 8 アプリがあります。
また、同じエラーが発生したコンソール アプリでサービスにアクセスしようとしました。
Win8 クライアントからサービスに送信しようとしている基本的なオブジェクトがありますが、HTTP 400 エラーが発生します。
サービスコード
[DataContract(Namespace="")]
public class PushClientData
{
[DataMember(Order=0)]
public string ClientId { get; set; }
[DataMember(Order=1)]
public string ChannelUri { get; set; }
}
[ServiceContract]
public interface IRecruitService
{
[OperationContract]
[WebInvoke(UriTemplate = "client")]
void RegisterApp(PushClientData app);
}
public class RecruitService : IRecruitService
{
public void RegisterApp(PushClientData app)
{
throw new NotImplementedException();
}
}
クライアントコード
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var data = new PushClientData
{
ClientId = "client1",
ChannelUri = "channel uri goes here"
};
await PostToServiceAsync<PushClientData>(data, "client");
}
private async Task PostToServiceAsync<T>(PushClientData data, string uri)
{
var client = new HttpClient { BaseAddress = new Uri("http://localhost:17641/RecruitService.svc/") };
StringContent content;
using(var ms = new MemoryStream())
{
var ser = new DataContractSerializer(typeof (T));
ser.WriteObject(ms, data);
ms.Position = 0;
content = new StringContent(new StreamReader(ms).ReadToEnd());
}
content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
var response = await client.PostAsync(uri, content);
response.EnsureSuccessStatusCode();
}
私は何を間違っていますか?
私はフィドラーでリクエストを見てきましたが、それは
http://localhost:17641/RecruitService.svc/client
そうあるべきだと思いますが、リターンは毎回エラー 400 (Bad request) です。
編集
Fiddler からの生のリクエストは以下のとおりです。を追加しました。localhost の後、Fiddler がそれを取得します。私たちがそれを取るか、そのままにしておくと、同じエラーが発生します。
POST http://localhost.:17641/RecruitService.svc/clients HTTP/1.1
Content-Type: text/xml
Host: localhost.:17641
Content-Length: 159
Expect: 100-continue
Connection: Keep-Alive
<PushClientData xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ClientId>client1</ClientId>
<ChannelUri>channel uri goes here</ChannelUri>
</PushClientData>