我在便携式类库上使用 HttpClient 获得 403

I#39;m getting 403 with HttpClient on Portable Class Library(我在便携式类库上使用 HttpClient 获得 403)
本文介绍了我在便携式类库上使用 HttpClient 获得 403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PCL、WP 和 WinStore 项目.在 PCL 项目中,我有一个使用这种方法的类:

I have a PCL, WP and WinStore projects. In the PCL project I have a class with this method:

    private async Task<string> GetIpAddress()
    {
        const string url = "http://www.ip-adress.com/";
        const string buscar = "<h3>Your IP address is:";

        var client = new HttpClient();
        var data = await client.GetStringAsync(url);
        if (data.IndexOf(buscar, StringComparison.Ordinal) <= -1) return;
        var IpAddress = data.Remove(0, data.IndexOf(buscar, StringComparison.Ordinal) + buscar.Length + 1);
        IpAddress = IpAddress.Remove(IpAddress.IndexOf("</h3>", StringComparison.Ordinal));
        return IpAddress;
    }

当我在 Windows Phone 项目上调用该方法时,它可以完美运行,但在 Windows Store 项目中却不行.相反,我收到此错误消息:

When I invoke the method on the Windows Phone project works perfectly, but not in the Windows Store project. Instead, I'm getting this error message:

推荐答案

看来您需要凭据并改为指向 api,这就是它禁止"的原因

It seems you need credentials and to point at the api instead , that's why its "Forbiden"

尝试像浏览器一样调用它:来自另一个问题HttpClient Request like browser"

Try calling it like a browser : from this other question "HttpClient Request like browser"

void Main()
{

    GetIP("http://www.ip-adress.com/");

}

async void GetIP(string url){
    try{
    "Looking Up".Dump("OK");
    var x = await  GetResponse(url);
    x.Dump();
    }
    catch(Exception e){
        e.Dump("Problems:");
    }
}

private static async Task<string> GetResponse(string url)
{
    var httpClient = new HttpClient();

    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");

    var response = await httpClient.GetAsync(new Uri(url));

    response.EnsureSuccessStatusCode();
    using (var responseStream = await response.Content.ReadAsStreamAsync())
    //using (var decompressedStream = new System.IO.Compression.GZipStream(responseStream, CompressionMode.Decompress))
    using (var streamReader = new StreamReader(responseStream))
    {
        return streamReader.ReadToEnd();
    }
}

//注意:我把压缩注释掉了,

//Note :I commented out the compression,

这篇关于我在便携式类库上使用 HttpClient 获得 403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)