c# - Objective C 中的 Xamarin Studio 类 HttpWebRequest 替代品是什么?

标签 c# ios xamarin.ios xamarin

我是 Xamarin Studio 的新手。我需要在我用 Objective C 编写的代码中使用一个功能,该功能来自以前用 Xamarin(C#) 编写的另一个代码。

在 Xamarin 代码中,图像的字节流附加到 HttpWebRequest 实例。并发布到服务器上,以获得经过处理的图像。

我无法找到如何使用与 xamarin 中的代码相同的功能

using (Stream requestStream = httpWebRequest.GetRequestStream ()){}

是。

我的 Xamarin 代码如下:-

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 ();
            });
        }

通过互联网搜索,我可以收集和实现的是:-

我的 objective-c 代码如下:-

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");
        }
    }
}];

现在,当我在 Xcode 中运行代码时: 输出是:-

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x75a9ea0 {NSUnderlyingError=0x7189250 "bad URL", NSLocalizedDescription=bad URL}

任何帮助都会很棒。 两天以来我一直坚持这一点。

附言:- 用于从 Xamarin 应用程序代码中提取 POST 请求的 webService URL。 我所做的是 我在 Xamarin 中借助 Consol.WriteLine() 打印了 URL 字符串 然后在 Xcode 中使用准确的输出作为 NSMutableURLRequest

最佳答案

这是 ios 安全性。也许这对你有帮助 - Bad URL when requesting with NSURL

关于c# - Objective C 中的 Xamarin Studio 类 HttpWebRequest 替代品是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21828254/

相关文章:

ios - 附加PathComponent后路径不同(_ :)

c# - WPF 中组合元素的部分缩放

c# - 我可以给 C# 参数一个默认值吗?

ios - 将图像附加到电子邮件按钮

ios - iTunes 查找 API 在我的应用程序中返回旧数据

mono - 获取密码字段的 Monotouch.Dialog InputElement

c# - 使用 MonoTouch 访问 WebScriptObject

silverlight - 是否有 Silverlight/wp7 的 MonoTouch.Dialog 端口?

c# - 如何在 RestSharp 中发送 POST 请求?

c# - 使用 ServiceStack 支持压缩请求体