Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从iOS向RESTful WCF服务.NET 4.0发布HTTP请求_C#_.net_Ios_Objective C_Wcf - Fatal编程技术网

C# 从iOS向RESTful WCF服务.NET 4.0发布HTTP请求

C# 从iOS向RESTful WCF服务.NET 4.0发布HTTP请求,c#,.net,ios,objective-c,wcf,C#,.net,Ios,Objective C,Wcf,我试图创建一个发送到WCF web服务的POST请求,该服务是用C#编写的,运行在IIS7.NETFramework4.0上 web服务适用于GET请求,但我似乎无法使POST方法正常工作。一些背景是,我在不得不切换到.NET之前,在服务器端使用PHP iOS中我的请求的代码: NSArray *jsonKeys = [NSArray arrayWithObjects:@"zip", nil]; NSArray *jsonValues = [NSArray arrayWithObjects: z

我试图创建一个发送到WCF web服务的POST请求,该服务是用C#编写的,运行在IIS7.NETFramework4.0上

web服务适用于GET请求,但我似乎无法使POST方法正常工作。一些背景是,我在不得不切换到.NET之前,在服务器端使用PHP

iOS中我的请求的代码:

NSArray *jsonKeys = [NSArray arrayWithObjects:@"zip", nil];
NSArray *jsonValues = [NSArray arrayWithObjects: zipcode, nil];
NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:jsonValues forKeys:jsonKeys];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/weather", ConnectionString3]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:jsonData];
WCF Web服务的C#代码:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(string zip);

通过创建一个类,使JSON映射到该类,可以解决这个问题

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(ZipCodeJSON zipJson);

[DataContract]
public class ZipCodeJSON
{
    [DataMember]
    public string zip { get; set; }
}
[运营合同]
[WebInvoke(Method=“POST”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare,UriTemplate=“/weather”)]
列出GetWeatherMethod(ZipCodeJSON-zipJson);
[数据合同]
公共类ZipCodeJSON
{
[数据成员]
公共字符串zip{get;set;}
}

问题在于您将操作的主体样式声明为
Bare
,但您正在发送希望参数接收的数据,并将其包装在参数名称中

如果将操作声明为

[OperationContract]
[WebInvoke(Method = "POST",
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.WrappedRequest,
           UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(string zip);
[运营合同]
[WebInvoke(Method=“POST”,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.WrappedRequest,
UriTemplate=“/weather”)]
列表GetWeatherMethod(字符串zip);

您可以根据需要将请求正文作为
{“zip”:“30309”}
发送。

您可以从服务器发布更多调试输出吗?像webmethod被执行过吗?遇到了什么错误?我在哪里可以找到服务器上的详细日志?我似乎只有一个日志文件,其中显示了向服务器发出的每个请求以及来自哪个ip的请求。据我所知,它没有更新登录。我不想在日志中看到“400”,请尽可能添加响应标题和内容。您所说的响应标题和内容是什么意思?如果您指的是最后三个字段所代表的内容(400 00),则它们是(sc状态、sc子状态、sc-win32-status和所用时间)。如果您的意思是我发送的json字符串是什么,那就是{“zip”:“30309”}
[OperationContract]
[WebInvoke(Method = "POST",
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.WrappedRequest,
           UriTemplate = "/weather")]
List<Weather> GetWeatherMethod(string zip);