在将xml作为帖子发送到Web服务时获取响应代码400

时间:2012-01-18 11:43:37

标签: iphone ios post nsurlrequest

我正在尝试使用基于iPhone的asp.net wcf构建的Web服务以xml的形式发布一些数据,但我收到错误代码400.我检查了它的描述,它从中看起来我是无法创建正确的请求。有人可以帮我纠正下面的代码:

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:
server有一个方法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 {     private int _TimesheetHourId;     private int _TimesheetId;     private DateTime _WorkedOn;     private int _PlacementOvertimeId;     private decimal _HoursWorked;     private string _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字符串)     {         回归&#34;成功&#34 ;;     }

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,其请求类型定义为XML 感谢

2 个答案:

答案 0 :(得分:0)

可能这是错误的格式 尝试编辑这些行:

<?xml version="1.0" encoding="iso-8859-1"?>
<TimesheetHour>
 <ContractNote>aaa</ContractNote>
 <TimesheetHourId>bbbb</TimesheetHourId>
 <TimesheetId>cccc</TimesheetId>
 <WorkedOn>dd</WorkedOn>
 <PlacementOvertimeId>eee<PlacementOvertimeId>
 <HoursWorked>ffff</HoursWorked>
 <PerformancevalueId>ggg</PerformancevalueId>
</TimesheetHour>

还要确保您没有将字符串传递给需要整数值的参数。 这是我可以帮助你而不深入的全部。

看到您编辑的内容是您的Web服务中定义了Ids。而不是传递数字或者如果你传递像@“%i,iValue”那么你创造的东西就像这样。至少尝试传递数字。

答案 1 :(得分:0)

获取WireSharkCharles并更详细地检查您的请求和服务器响应。 WireShark是免费的,X11和Charles有30个试用版,并且是原生的。

我个人使用Charles,下载,启动并且它正在捕获您的网络流量,就这么容易。