Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有基本身份验证的c#http post请求_C# - Fatal编程技术网

具有基本身份验证的c#http post请求

具有基本身份验证的c#http post请求,c#,C#,我已经编写了一个python函数,通过HTTP post请求和基本Authentivation移动路由器的IO端口。这个很好用。但是现在我想用C#实现sam 下面是我的python函数: def io_on(ip='192.168.2.1', username='adm', password='123456'): if not isinstance(ip, str): print('not string') try: payload ='_ajax=1&_web_cmd=

我已经编写了一个python函数,通过HTTP post请求和基本Authentivation移动路由器的IO端口。这个很好用。但是现在我想用C#实现sam

下面是我的python函数:

def io_on(ip='192.168.2.1', username='adm', password='123456'):
if not isinstance(ip, str):
    print('not string')
try:
    payload ='_ajax=1&_web_cmd=%21%0Aio%20output%201%20on%0A'
    r = requests.post('http://{}/apply.cgi'.format(ip), auth=HTTPBasicAuth(username, password), data=payload, timeout=3)
    if r.status_code == 200:
        print('{} : IO ON'.format(ip))
    elif r.status_code == 401:
        print('{} : Auth error'.format(ip))
    else:
        print(r.status_code)

except Exception as e:
    print(e)

我尝试过网络认证,但没有成功

类似这样:

    try
    {
        string username = "adm", password = "123456";
        string payload = "http://192.168.2.1/apply.cgi/?_ajax=1&_web_cmd=%21%0Aio%20output%201%20on%0A";


        HttpClient client = new HttpClient();

        var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

        HttpResponseMessage response = await client.GetAsync(payload);
        HttpContent content = response.Content;

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Success");
        }

        else if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            Console.WriteLine("Auth error");
        }
        else
        {
            Console.WriteLine(response.StatusCode);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }

大概是这样的:

    try
    {
        string username = "adm", password = "123456";
        string payload = "http://192.168.2.1/apply.cgi/?_ajax=1&_web_cmd=%21%0Aio%20output%201%20on%0A";


        HttpClient client = new HttpClient();

        var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

        HttpResponseMessage response = await client.GetAsync(payload);
        HttpContent content = response.Content;

        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("Success");
        }

        else if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            Console.WriteLine("Auth error");
        }
        else
        {
            Console.WriteLine(response.StatusCode);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }

下面是我使用基本身份验证制作帖子的方法

var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{login}:{password}")));

using (var client = new HttpClient() { DefaultRequestHeaders = { Authorization = authValue } })
{
    HttpResponseMessage response = client.PostAsync("https://localhost:44396/Documentation/All?pageNumber=0&pageSize=10", httpContent).Result;
    if (response.IsSuccessStatusCode)
    {
        response = await response.Content.ReadAsStringAsync();
    }
}

下面是我使用基本身份验证制作帖子的方法

var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{login}:{password}")));

using (var client = new HttpClient() { DefaultRequestHeaders = { Authorization = authValue } })
{
    HttpResponseMessage response = client.PostAsync("https://localhost:44396/Documentation/All?pageNumber=0&pageSize=10", httpContent).Result;
    if (response.IsSuccessStatusCode)
    {
        response = await response.Content.ReadAsStringAsync();
    }
}

这看起来像是根据您的粘贴得到的不是帖子。您可以调整它吗?只需更改此行请求。Method=“POST”;请求。方法=“获取”;我做到了,谢谢。我仍然收到一个未经授权的包裹。我现在是这样做的:你仍然在使用POST这看起来像是一个GET而不是一个POST,根据你的粘贴,你可以调整它吗?只需更改此行请求。Method=“POST”;请求。方法=“获取”;我做到了,谢谢。我仍然收到一个未经授权的包裹。我现在是这样做的:您仍然在使用POST