首页 文章

ASIHTTPRequest WCF POST XML

提问于
浏览
0

我正在尝试让我的应用程序将数据发送到WCF RESTful Web服务 .

此时我没有从ASIHTTPRequest收到任何错误消息,但Web服务也没有做任何事情 .

你们可以看看它,看看你能发现什么吗?

Xcode中

NSURL *url = [NSURL URLWithString:@"http://www.mydomian.com/create"];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

NSData *myPostData = [[NSString        stringWithFormat:@"<Product><Description>desc1</Description><Id></Id><Name>some    body</Name></Product>"] dataUsingEncoding:NSUTF8StringEncoding];

NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];

[request setPostBody:myMutablePostData];

[request setRequestMethod:@"POST"];

[request addRequestHeader:@"Content-Type" value:@"application/xml"];

[request setDelegate:self];

[request startSynchronous];

网络服务

...

public class Product {[DataMember] public string Id {get;组; }

[DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }
}

...

[OperationContract] [WebInvoke(UriTemplate =“/ create”,Method =“POST”,RequestFormat = WebMessageFormat.Xml,BodyStyle = WebMessageBodyStyle.Bare)] void CreateProduct(Product product);

...

... ... CreateProduct

myConnection.Open();

string insertString = "insert into tbldata (desc,Name) values (@desc,@Name)";
            MySqlCommand myCommand = new MySqlCommand(insertString, myConnection);

            myCommand.Parameters.Add("@desc", MySqlDbType.VarChar, 12);
            myCommand.Parameters.Add("@Name", MySqlDbType.VarChar, 40);

            myCommand.Parameters["@desc"].Value = product.Description;
            myCommand.Parameters["@Name"].Value = product.Name;


            myCommand.ExecuteNonQuery();

1 回答

  • 0

    您的请求看起来没问题,但您可以尝试使用其他工具测试它,例如命令行中的 curl . 这是尝试的东西:

    curl -D headers.txt -X POST -H 'Content-Type:application/xml' -d '<Product><Description>desc1</Description><Id></Id><Name>somebody</Name></Product>' http://www.mydomain.com/create
    

    检查响应正文的stdout和响应 Headers 的 headers.txt . 你应该得到你在iOS应用程序中获得的相同的东西 .

    在服务器端响应方面,尝试使用不同的状态代码或正文进行响应 .

相关问题