简体   繁体   中英

Webview Status Code in Xamarin Android

I checked this following java resource to get the status code of Webview for Xamarin Android from c#. As from Xamarin documentation WebResourceResponse(String, String, Stream) Stream api is of type System.IO.Stream . I need to convert Java.IO.InputStream to System.IO.Stream .

Is it possible convert these type?. if not what could be the possible fix. I am new to 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.

Replace

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

with:

MemoryStream stream = new MemoryStream(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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