简体   繁体   English

Xamarin Android中的Webview状态代码

[英]Webview Status Code in Xamarin Android

I checked this following java resource to get the status code of Webview for Xamarin Android from c#. 我检查了以下Java 资源,以从c#获取Xamarin Android的Webview的状态代码。 As from Xamarin documentation WebResourceResponse(String, String, Stream) Stream api is of type System.IO.Stream . 从Xamarin文档开始, WebResourceResponse(String,String,Stream) Stream api的类型为System.IO.Stream I need to convert Java.IO.InputStream to System.IO.Stream . 我需要将Java.IO.InputStream转换为System.IO.Stream

Is it possible convert these type?. 是否可以转换这些类型? if not what could be the possible fix. 如果没有,可能是什么解决办法。 I am new to Xamarin. 我是Xamarin的新手。 Please help. 请帮忙。

private static string http = "https?://[\\w\\.\\-]+(/.*)?";
Regex regex = new Regex(http);

public override WebResourceResponse ShouldInterceptRequest(WebView webView, string url)
{
    if (!regex.IsMatch(http))
    {
        return ShouldInterceptRequest(webView, url);
    }

    HttpGet req= new HttpGet(url);
    DefaultHttpClient client = new DefaultHttpClient();

    String mimetype = null, encoding = null;

    byte[] data = null;

    try
    {
        var response = client.Execute(req); 

        if (HttpStatus.ScOk == response.StatusLine.StatusCode)
        {
            var httpentity = response.Entity;
            var header = httpentity.ContentType;

            if (null != mimetype) mimetype = header.Value;

            var encodingheader = httpentity.ContentEncoding;
            if (null != encodingheader) encoding = encodingheader.Value;

            data = EntityUtils.ToByteArray(httpentity);
        }
    }
    catch (Exception e)
    {
        String msg = e.Message;
        Log.Error(this.Class.SimpleName, (null != msg) ? msg : "");
    }
    finally
    {
        req.Abort();
        client.ConnectionManager.Shutdown();
    }

    Java.IO.InputStream stream = new ByteArrayInputStream(data);

    // this is the error i get
    // cannot convert from 'Java.IO.InputStream' to 'System.IO.Stream'
    // Argument type 'Java.IO.InputStream' is not assignable to parameter type 'System.IO.Stream'
    return new WebResourceResponse(mimetype, encoding, stream);

}

Is it possible convert these type? 是否可以转换这些类型?

The answer is No. You can't convert directly, you can use file to achieve it, but it will make the problem complex. 答案是否定的。您不能直接转换,可以使用文件来实现,但是这会使问题变得复杂。

In your problem, you can use MemoryStream class, it can accept Byte[] as parameter in its construct. 在您的问题中,可以使用MemoryStream类,它可以在其构造中接受Byte[]作为参数。

Replace 更换

Java.IO.InputStream stream = new ByteArrayInputStream(data);

with: 与:

MemoryStream stream = new MemoryStream(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM