【问题标题】:What is Xamarin Studio's class HttpWebRequest replacement in Objective C?什么是 Xamarin Studio 在 Objective C 中的类 HttpWebRequest 替换?
【发布时间】:2014-02-18 18:56:33
【问题描述】:

我对 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}

任何帮助都会很棒。 两天以来我一直被困在这个问题上。

PS :- 用于从 Xamarin App Code 中提取 POST 请求的 webService URL。 我所做的是 我在 Xamarin 中的 Consol.WriteLine() 的帮助下打印了 URL 字符串 然后使用 Xcode 中的确切输出作为 NSMutableURLRequest

【问题讨论】:

  • 这是 ios 安全性。也许这对你有帮助 stackoverflow.com/questions/6287230/…stackoverflow.com/questions/10782591/…
  • 我不明白什么有效,什么无效。 ObjC 版本可以,但 C# 版本不行?或相反亦然?如果您在 Xcode 中遇到错误,那么您显然没有使用 Xamarin - 我很困惑。
  • 他想将 C# 代码转换为 ObjC。由于并非所有代码都可用,因此很难确定哪里出了问题。这是您尝试访问的服务器地址吗:“webserviceNameBaseAddress”

标签: c# ios xamarin.ios xamarin


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多