我是 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/