gpt4 book ai didi

c# - 如何将此 Perl 上传脚本转换为 C#?

转载 作者:行者123 更新时间:2023-11-30 17:59:59 25 4
gpt4 key购买 nike

我目前正在尝试将我的 Perl 上传脚本移植到 C#,但我对这门语言不太熟悉,无法让它工作。我已经尝试了很长时间,但似乎没有任何效果。我感谢任何帮助。 :)

Perl 版本:

#!/usr/bin/perl -w
use strict;
use warnings;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request::Common;
my ($user, $pass, $type, $fileName);

# This chapter sets some vars and parses some vars.
$/ = undef;
$SIG{PIPE} = 'IGNORE';
$fileName = $ARGV[0] || die "Syntax: $0 <filename to upload> <free|prem|col> [login] [password]\n";
$type = $ARGV[1] || "";
$user = $ARGV[2] || "";
$pass = $ARGV[3] || "";

# RapidShare API Documentation @ http://images.rapidshare.com/apidoc.txt

my $nextuploadserver = get('http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver');
die 'nextuploadserver broken' if $nextuploadserver !~ /^\d+$/;
print 'nextuploadserver:' . $nextuploadserver . "\n";

my $userAgent = LWP::UserAgent->new();
my $request = POST 'http://rs' . $nextuploadserver . '.rapidshare.com/cgi-bin/rsapi.cgi',
Content_Type => 'form-data',
Content => [
sub => 'upload',
login => $user,
password => $pass,
filename => $fileName,
filecontent => [$fileName],
];
my $response = $userAgent->request($request);
die 'fucked up' if $response->is_error();
print $response->content . "\n";

C#,这就是我尝试过的:

WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "form-data";
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
string a = utf8.GetString(
wc.UploadData(
@"https://rs702l3.rapidshare.com/
cgi-bin/rsapi.cgi", "POST", System.Text.Encoding.UTF8.GetBytes(
"sub=upload&login=XXXX&password=XXXX&folder=0&filename=Test.jpg
&filecontent=" + utf8.GetString(File.ReadAllBytes(@"D:\Test.jpg")))));

Console.WriteLine("Test: " + a);

P.S.:如果有人正在寻找一个有效的 Rapidshare uplaod 脚本,您可以免费使用我在线程中发布的脚本。 :)

最佳答案

这是我们正在使用的一些 c# POST 代码:

string uri = "http://...";
string parameters = String.Format("param1={0}&param2={1}",
HttpUtility.UrlEncode(param1),
HttpUtility.UrlEncode(param2));

HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.AllowAutoRedirect = true;
webRequest.MaximumAutomaticRedirections = 1;

byte[] bytes = Encoding.ASCII.GetBytes(parameters);
Stream os = null;
try {
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
} catch (Exception ex) {
// Handle exception
} finally {
if (os != null) {
os.Close();
}
}

关于c# - 如何将此 Perl 上传脚本转换为 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com