我想使用建立在asp.net wcf從iPhone上的Web服務以xml的形式發佈一些數據,但我得到的錯誤代碼400.我已經檢查過它的描述,從它看來,我無法創建適當的請求。有人可以請幫我糾正我的代碼這是繼:得到響應代碼400,同時發送xml作爲張貼到Web服務
NSString *urlString = [NSString stringWithFormat:@"http://172.xxx.xxx.xxx:1230/Service.svc/updateTimesheet"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<TimesheetHour><ContractNote>111</ContractNote><TimesheetHourId>222</TimesheetHourId><TimesheetId>333</TimesheetId><WorkedOn>444</WorkedOn><PlacementOvertimeId>555</PlacementOvertimeId><HoursWorked>666</HoursWorked><PerformancevalueId>777</PerformancevalueId></TimesheetHour>"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);
//here you get the response
}
EDIT1:
服務器有一個希望將類作爲參數的方法updateTimesheet。該請求被定義爲XM和響應被定義爲JSON
EDIT2:下面
是我的WCF服務代碼:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "updateTimesheet", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
string UpdateTimesheetHours(string ts);
[DataContract] 公共類TimesheetHour { 私人詮釋_TimesheetHourId ; private int _TimesheetId; 私人DateTime _WorkedOn; private int _PlacementOvertimeId; 私人小數_HoursWorked; 私人字符串_contractorNote = string.Empty; private int _performancevalueId = 0;
[DataMember]
public string ContractorNote
{
get { return _contractorNote; }
set { _contractorNote = value; }
}
[DataMember]
public int TimesheetHourId
{
get { return _TimesheetHourId; }
set { _TimesheetHourId = value; }
}
[DataMember]
public int TimesheetId
{
get { return _TimesheetId; }
set { _TimesheetId = value; }
}
[DataMember]
public DateTime WorkedOn
{
get { return _WorkedOn; }
set { _WorkedOn = value; }
}
[DataMember]
public int PlacementOvertimeId
{
get { return _PlacementOvertimeId; }
set { _PlacementOvertimeId = value; }
}
[DataMember]
public decimal HoursWorked
{
get { return _HoursWorked; }
set { _HoursWorked = value; }
}
[DataMember]
public int PerformancevalueId
{
get { return _performancevalueId; }
set { _performancevalueId = value; }
}
} 公共字符串UpdateTimesheetHours(TimesheetHour字符串) { 回報 「成功」; }
EDIT3 我的XML
<TimesheetHour>
<ContractNote>111</ContractNote>
<TimesheetHourId>222</TimesheetHourId>
<TimesheetId>333</TimesheetId>
<WorkedOn>444</WorkedOn>
<PlacementOvertimeId>555</PlacementOvertimeId>
<HoursWorked>666</HoursWorked>
<PerformancevalueId>777</PerformancevalueId>
我呼籲服務器一個函數,它需要一個字符串作爲paremeter和請求類型 感謝
你可以包括髮送到服務器的實際文章?例如上面的代碼在您的應用中運行時產生的字符串。 – Diziet 2012-01-18 11:48:10
400錯誤表示您的請求格式有問題。如果不知道請求的內容細節(即服務器期望的內容),那麼很難知道自己做錯了什麼。 – 2012-01-18 11:52:57
NSLog將你傳遞給服務器的東西添加到你的問題中。可能那時我們可以幫助你。 – DivineDesert 2012-01-18 12:01:39