cURL调用C# [英] cURL call in C#

查看:182
本文介绍了cURL调用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行cURL请求才能从网络中抓取CSV文件。我从来没有使用cURL之前,迄今为止,我通过谷歌看到的东西似乎没有意义:



cURL调用需要看起来像这样: / p>

  curl --user用户名:password http://somewebsite.com/events.csv 



我想要使用的代码(我使用它来获取基本的auth XML文件)

  string URL =http://somewebsite.com/events.csv; 
string用户名=username;
string Password =password;
WebClient req = new WebClient();

CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(URL),Basic,new NetworkCredential(Username,Password));

req.Credentials = myCache;

string results = null;
results = Encoding.UTF8.GetString(req.DownloadData(URL));

这只是给我一个登录屏幕的html,所以认证不会发生。

解决方案

我发现了问题。显然,当您在用户名字段中传递@符号(如电子邮件用户名)时,请求失败,因此必须将其转换为基本64字符串。这里的代码只是让别人跑到这里:



string url =https://website.com/events.csv; WebRequest myReq = WebRequest.Create(url);

  string username =username 
string password =password;
string usernamePassword = username +:+ password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url),Basic,new NetworkCredential(username,password));
myReq.Credentials = mycache;
myReq.Headers.Add(Authorization,Basic+ Convert.ToBase64String(new ASCIIEncoding()。GetBytes(usernamePassword)));

WebResponse wr = myReq.GetResponse();
stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream,Encoding.UTF8);
string content = reader.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();


I need to perform a cURL request to grab a CSV file from the web. I've never used cURL before and so far the stuff I have seen via google doesn't seem to make sense:

the cURL call needs to look like this:

curl --user username:password http://somewebsite.com/events.csv

the code I am trying to use ( i use this to grab basic auth XML files)

string URL = "http://somewebsite.com/events.csv";
string Username = "username";
string Password = "password";
WebClient req = new WebClient();

CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(URL), "Basic", new NetworkCredential(Username, Password));

req.Credentials = myCache;

string results = null;
results = Encoding.UTF8.GetString(req.DownloadData(URL));

This just gives me back the html for a login screen so the authentication is not happening.

解决方案

I found the issue. Apparently the request fails when you pass an "@" sign ( like in the email username ) in the username field so it must be Converted to a base 64 string. Here is the code just incase someone else runs into this:

string url = "https://website.com/events.csv"; WebRequest myReq = WebRequest.Create(url);

    string username = "username";
    string password = "password";
    string usernamePassword = username + ":" + password;
    CredentialCache mycache = new CredentialCache();
    mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
    myReq.Credentials = mycache;
    myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

    WebResponse wr = myReq.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();
    Console.WriteLine(content);
    Console.ReadLine();

这篇关于cURL调用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆