2

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40
  • 1
    this is ios security. maybe this help you http://stackoverflow.com/questions/6287230/bad-url-when-requesting-with-nsurl or http://stackoverflow.com/questions/10782591/nsurlerrordomain-code-1000-bad-url-what-does-it-really-mean – Igor Antsiferov Feb 17 '14 at 12:39
  • I don't understand what is working and what not. The ObjC version is OK but the C# version not? Or vice versa? If you get an error in Xcode, you're obviously not using Xamarin - I'm confused. – Krumelur Feb 17 '14 at 14:33
  • He wants to convert C# code to ObjC. It is hard to determine what is wrong since not all the code is available. Is this the server address you are trying to reach: "webserviceNameBaseAddress" – SKall Feb 17 '14 at 16:12

1 Answers1

0

this is ios security. maybe this help you - Bad URL when requesting with NSURL

Community
  • 1
  • 1
Igor Antsiferov
  • 273
  • 2
  • 9