如何在使用asformdatarequest时实现c#服务器端

本文关键字:实现 服务器端 asformdatarequest | 更新日期: 2023-09-27 17:53:55

我试图使用asformdatarequest将数据发送到ASP.net服务器端。我已经创建了一个aspx页面。目前我可以得到两个纯文本。然而,我不知道如何在c#中通过Request.Form来修饰NSdata。

下面是Obj-C代码:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"name"];
[request setPostValue:@"Copsey" forKey:@"code"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

这是当前c#代码:

 string name = Request.Form["name"] == null ? "" : Request.Form["name"];
 string code = Request.Form["code"]==null?"":Request.Form["code"];

如你所见,在iphone中,我尝试发送图像到c#服务器端,但我不知道怎么做?

如何在使用asformdatarequest时实现c#服务器端

使用asformdatarequest将图像发送到WCF REST服务。下面是我们在生产中的一个项目的例子…

假设我有一个UIImage在var中叫做image

NSString *surl = @"http:www.SomeRestService.com"    
NSURL *url = [NSURL URLWithString:surl];
ASIFormDataRequest *r = [ASIFormDataRequest requestWithURL:url];
[r setValidatesSecureCertificate:NO];
[r setTimeOutSeconds:30];
[r setRequestMethod:@"POST"]; //default is POST (insert), 
[r setDelegate:self];
[r setDidFailSelector:@selector(requestDidFail:)];
//[r addRequestHeader:@"Content-Type" value:@"application/json"]   this will cause the call to fail.  No content-type header for this call.

NSMutableData *imageData = [NSMutableData dataWithData:UIImageJPEGRepresentation(image, .35)]; //we are really compressing our images.. you can do what you want, of course.
[r setPostBody:imageData];
[r setDidFinishSelector:@selector(imageSaveDidFinish:)];
[r startAsynchronous];

好的,在WCF端,您需要定义一个接收System.IO的方法。Stream,并且Stream需要是最后定义的参数,它应该是POST,并且不应该包含任何其他参数作为POST主体的一部分(您可以在URL和查询字符串中定义参数,尽管一些纯粹主义者会说这对于REST POST来说是不好的形式)。

[WebInvoke(UriTemplate = "Upload", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
        public GenericObject SaveReceiptImage(System.IO.Stream imageStream)
        {
                            try
            {
                byte[] buffer = new byte[16 * 1024];
                using (MemoryStream ms = new MemoryStream())
                {
                    int read = 0;
                    while ((read = imageStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    ms.Position = 0;
                    if (ms.Length > 0)
                    {
                      //save your byte array to where you want
                    }
                    else
                    {
                      // woops, no image was passed in
                    }
                }
            }
            catch (Exception ex)
            {
                //bad error occured, log it
            }
            return whatever;
        }

请查看以下链接:

http://allseeing-i.com/ASIHTTPRequest/How-to-use

http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/29519-help-http-post-using-asiformdatarequest.html