考虑:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//int[] val = { 0, 0};
int val;
if (textBox1.Text == "")
{
MessageBox.Show("Input any no");
}
else
{
val = Convert.ToInt32(textBox1.Text);
Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
ot1.Start(val);
}
}
private static void ReadData(object state)
{
System.Windows.Forms.Application.Run();
}
void setTextboxText(int result)
{
if (this.InvokeRequired)
{
this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
}
else
{
SetTextboxTextSafe(result); …
Run Code Online (Sandbox Code Playgroud) 使用cURL,我们可以使用HTTP Web请求传递用户名,如下所示:
$ curl -u <your_username> https://api.github.com/user
Run Code Online (Sandbox Code Playgroud)
该-u
标志接受用于身份验证的用户名,然后cURL将请求密码.cURL示例用于使用GitHub Api进行基本身份验证.
我们如何同样传递用户名和密码以及Invoke-WebRequest?最终目标是在GitHub API中使用基本身份验证的PowerShell.
Notes来自客户端的Wikipedia on Basic Auth.
将用户名和密码合并为一个字符串 username:password
$user = "shaunluttin"
$pass = "super-strong-alpha-numeric-symbolic-long-password"
$pair = "${user}:${pass}"
Run Code Online (Sandbox Code Playgroud)
将字符串编码为Base64的RFC2045-MIME变体,但不限于76个字符/行.
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
Run Code Online (Sandbox Code Playgroud)
创建Auth值作为方法,空格,然后编码对 Method Base64String
$basicAuthValue = "Basic $base64"
Run Code Online (Sandbox Code Playgroud)
创建标题 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
$headers = @{ Authorization = $basicAuthValue }
Run Code Online (Sandbox Code Playgroud)
调用Web请求
Invoke-WebRequest -uri "https://api.github.com/user" -Headers $headers
Run Code Online (Sandbox Code Playgroud)
感谢@briantist的帮助!
PowerShell版本比cURL版本更冗长.这是为什么?@briantist指出GitHub打破了RFC并且PowerShell坚持使用它.这是否意味着cURL也打破了标准?
我需要检查存在的某些属性.喜欢:
if "blah-blah-blah" is None:
print "there is no such attribute"
else:
print "The attribute exists"
Run Code Online (Sandbox Code Playgroud)