提问人:Muju 提问时间:1/17/2017 最后编辑:Muju 更新时间:1/17/2017 访问量:49
为什么在目标 c 中将图像发送到服务器时获取服务器响应器 0
Why getting server responser 0 when sending image to server in objective c
问:
我是iOS的新手,我面临着将图像发送到Web服务的问题。 我的代码是这样的
UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
[[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);
NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImage), 0.5)];
SignString = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
int imageSize = imgData.length;
NSLog(@"size of image in KB: %d ", imageSize/1024);
我正在使用签名视图,并将其转换为图像,然后将其转换为字符串,然后将其发送到 Web 服务。
我的 Web 服务是这样的
POST /webservice.asmx HTTP/1.1
Host: url
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Method"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Method xmlns="http://tempuri.org/">
<ID>string</ID>
<NameId>long</NameId>
<Sign1>string</Sign1>
<Sign2>string</Sign2>
<Email>string</Email>
<ClientName>string</ClientName>
<UserId>int</UserId>
<CreatedDate>string</CreatedDate>
</Method>
</soap:Body>
</soap:Envelope>
我像这样将其发送到网络服务
-(void)serverconnectionPost{
NSString *MethodString =@"?op=Method";
NSString *ServerfullString =[NSString stringWithFormat:@"%@%@",ServerString,MethodString];
NSString *id=@"0";
NSString *EmailString=emailtxt.text;
NSString *NameString=clientnametxt.text;
long nameid=[AuditNameId longLongValue];
int UserId=[userid intValue];
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<Method xmlns=\"http://tempuri.org/\">"
"<Id>%@</Id>"
"<NameId>%ld</NameId>"
"<Sign1>%@</Sign1>"
"<Sign2>%@</Sign2>"
"<Email>%@</Email>"
"<ClientName>%@</ClientName>"
"<UserId>%d</UserId>"
"<CreatedDate>%@</CreatedDate>"
"</Insert_Audit_Signs>"
"</soap:Body>"
"</soap:Envelope>",id,nameid,Sign1,Sign2,EmailString,ClientNameString,UserId,AuditStartDate];
NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:ServerfullString]];
[request setHTTPMethod:@"POST"];
[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"URL = %@",request);
[request setHTTPBody:postData];
myNSUConnectionPOSTObj = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection link =%@",myNSUConnectionPOSTObj);
if(myNSUConnectionPOSTObj) {
myNSMDataFromPOSTServer =[[NSMutableData alloc] init];
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[myNSMDataFromPOSTServer setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[myNSMDataFromPOSTServer appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *myNSXMLParserPostObj=[[NSXMLParser alloc]initWithData:myNSMDataFromPOSTServer];
myNSXMLParserPostObj.delegate=self;
[myNSXMLParserPostObj parse];
NSLog(@"%@",myNSXMLParserPostObj.parserError);
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: myNSMDataFromPOSTServer encoding:NSUTF8StringEncoding];
//NSLog(@"Response from Server : %@", responseStringWithEncoded);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
NSString *Str =serverResponse.text;
NSLog(@"Server Response =%@",Str);
alert3 = [[UIAlertView alloc] initWithTitle:@"Insert Data"
message:@"Check List Save Successfully"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK",nil];
//[alert3 setTag:1];
[alert3 show];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
myMutableStringPOSTObj=[[NSMutableString alloc]initWithString:string];
NSLog(@"Array String: %@",myMutableStringPOSTObj);
NSData *dataPost = [myMutableStringPOSTObj dataUsingEncoding:NSUTF8StringEncoding];
responsePOSTdict = [NSJSONSerialization JSONObjectWithData:dataPost options:0 error:nil];
NSLog(@"JSON DATA = %@",responsePOSTdict);
}
我添加了这么多代码,因为我没有到达我的地方并且做错了。其他 Web 服务工作正常。但是在这个网络服务中,我得到的响应是0。提前致谢!
答: 暂无答案
评论