简体   繁体   English

从iPhone应用程序将数据发布到ASPX页面

[英]Post data to aspx page from iphone application

I am developing a application. 我正在开发一个应用程序。 In which i am posting a image to .aspx page.The HTML for the page is as below. 我在其中将图像发布到.aspx页面。页面的HTML如下。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>">
<html xmlns="http://www.w3.org/1999/xhtml>">
<head><title>
Untitled Page
</title><link href="App_Themes/XXX/XXX.css" type="text/css" rel="stylesheet" /></head>
<body>
    <form name="form1" method="post" action="Default16.aspx" id="form1" enctype="multipart/form-data">    
<div>    
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTQwMjY2MDA0Mw9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm0tZGF0YWRktr+hG1VVXZsO01PCyj61d6Ulqy8=" />
</div>
<div>
     <div style="float:left;margin:10px">
       <input type="file" name="fuImage" id="fuImage" />
     </div>
     <div style="float:right">
            <input type="submit" name="btnPost" value="Post Image" id="btnPost" />
     </div>
   </div>
    </form>
</body>
</html>

Now i am sending a request from my application then i am getting " Error Domain=kCFErrorDomainCFNetwork Code=303 UserInfo=0xf541c0 "Operation could not be completed. 现在,我正在从应用程序发送请求,然后得到“错误域= kCFErrorDomainCFNetwork代码= 303 UserInfo = 0xf541c0“操作无法完成。 (kCFErrorDomainCFNetwork error 303.)"" (kCFErrorDomainCFNetwork错误303。)“”

I have tried using SynchronousRequest and aSynchronousRequest but both are not working. 我曾尝试使用SynchronousRequest和aSynchronousRequest,但两者均无法正常工作。 I have also used apple sample code. 我还使用了苹果示例代码。

Here is the code for iPhone app 这是iPhone应用程序的代码

UIImage *image=[UIImage imageNamed:@"photo2.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 90);
NSString *urlString = @"http://XXXXXXXX.com/Post.aspx";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"----WebKitFormBoundarylU9pAl5wPrF+Tk52"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"fuimage\"; filename=\"asd.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
// NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
// NSLog(returnString);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}

As a general rule, if you want to post data from an external source (other than aspx, which does some crazy stuff) you are much better with a simple "handler" (ashx) or an ASP.NET MVC route. 通常,如果要从外部源发布数据(aspx除外,它做一些疯狂的事情),则使用简单的“处理程序”(ashx)或ASP.NET MVC路由要好得多。 In the "handler" case you can access the request form/query-string etc directly. 在“处理程序”情况下,您可以直接访问请求表单/查询字符串等。 In the MVC case it should mostly be done for you. 在MVC情况下,通常应该为您完成。

aspx is not a friend to raw posts. aspx不是原始帖子的朋友。

In the specific case, kCFErrorDomainCFNetwork suggests some kind of network error, but you would need a http trace to identify it for sure. 在特定情况下, kCFErrorDomainCFNetwork 建议某种网络错误,但是您需要使用http跟踪来确定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM