我有一個Windows 8應用程序試圖訪問RESTful WCF服務。從Win8應用程序到WCF服務HttpClient.PostAsync給錯誤400錯誤請求
我也嘗試使用控制檯應用程序訪問具有相同錯誤的服務。
我有一個基本的對象,我試圖發送到我的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(錯誤請求)。
編輯
將從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>
確保您的客戶端是正確的(在桌面應用程序中嘗試)。其他要嘗試的事情是[啓用回送](http://msdn.microsoft.com/en-us/library/windows/apps/Hh780593.aspx),並在防火牆中插入一個洞。 –
你可以包含來自fiddler的RAW HTTP請求嗎? –
你有沒有試過編碼角度大括號? –