xamarin.ios - call webservice using NSMutableURLRequest in monotouch -
i try save capture image iphone server. done xcode. code like
nsmutableurlrequest *request= [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"post"]; nsstring *boundary = @"---------------------------14737809831466499882746641449"; 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", boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: attachment; name=\"%@\" filename=\"test.png\"\r\n", imgname] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-type: application/octet-stream\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:[nsdata datawithdata:imagedata]]; [body appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; [request sethttpbody:body]; nsurlconnection *theconnection = [[nsurlconnection alloc] initwithrequest:request delegate:delegate];
now try monotouch,
here create connection, send request & receive response working fine.
but here don't know how set header & body nsmutableurlrequest, need know how pass parameters (like above xcode).
nsmutableurlrequest request = new nsmutableurlrequest(new nsurl("http://url.com"), nsurlrequestcachepolicy.reloadrevalidatingcachedata, 20); request.httpmethod = "post"; var connectiondelegate = new testnsurlconnectiondelegate(); var connection = new nsurlconnection(request, connectiondelegate); connection.start();
can 1 me this...
using system.net access httpwebrequest.
you have post url.. , write byte[] of image body of request.
in example.. servicebase value maps static string, , "service.submitimage" static string pointing service method endpoint.
public void submitimage(byte[] image) { var request = webrequest.create(servicebase + service.submitimage); request.method = "post"; request.contenttype = "application/octet-stream"; request.contentlength = image.length; stream str = request.getrequeststream(); str.write(image, 0, image.length); str.close(); httpwebresponse response = (httpwebresponse)request.getresponse(); }
Comments
Post a Comment