I am pretty much new to Xamarin Studio. and i need to use a functionality in my code which i am writing in Objective C, from another code which in Previously written in Xamarin(C#).
In the Xamarin Code a byte stream of an Image is getting attached to an HttpWebRequest instance. and posted on a server, in order to get a processed image.
I am being unable to find how to use same functionality as the code in xamarin
using (Stream requestStream = httpWebRequest.GetRequestStream ()){}
Does.
my Xamarin Code is As Follows :-
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create (string.Concat (new string[]
{
Common.BaseAddress,
"visualize/",
GenericHelper.ServiceEncrypt (Common.Token),
"/",
Common.AppTag,
"/",
Common.ConstructTreatmentSelections ()
}));
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
using (Stream requestStream = httpWebRequest.GetRequestStream ())
{
int num = 2048;
int i = 1;
int num2 = pBytePhotoImage.Length;
while (i < num2)
{
if (i + num > num2)
{
num = num2 - i;
}
requestStream.Write (pBytePhotoImage, i - 1, num);
float fProgress = (float)i / (float)num2;
if (fProgress <= 1f)
{
nSAutoreleasePool.InvokeOnMainThread (delegate
{
pProgressBar.set_Progress (fProgress);
});
}
else
{
nSAutoreleasePool.InvokeOnMainThread (delegate
{
pProgressBar.set_Progress (1f);
});
}
i += num;
}
nSAutoreleasePool.InvokeOnMainThread (delegate
{
pAnimationImage.StartAnimating ();
});
}
From searching over the internet what i could gather and implemented was :-
my Objective C code is as follows :-
NSData *data = UIImageJPEGRepresentation(self.imageView.image, 0.2);
NSString *webServiceURL = @"https://webserviceNameBaseAddress/visualize/lEr0WKCdarv2FLGFvBRbhs41FN7zhgcdiwuEqFtLQINJ|PQpM8HEGQ~~/ios/SL|VL|OC|ML|";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:webServiceURL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:data];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData * responseData, NSError *err) {
NSLog(@"URL Response");
if (err) {
NSLog(@"Err %@",err.description);
}else
{
if (responseData) {
NSLog(@"%@",[UIImage imageWithData:responseData]);
}
else{
NSLog(@"not returning anything");
}
}
}];
Now when i run the code in Xcode : the output is :-
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x75a9ea0 {NSUnderlyingError=0x7189250 "bad URL", NSLocalizedDescription=bad URL}
Any help would be great. I am stuck at this since two days.
PS :- for extracting the webService URL for POST request , from Xamarin App Code. what i did was i have printed the URL string out with the help of Consol.WriteLine() in Xamarin and then used the exact output in Xcode as NSMutableURLRequest