如何发送base64 image vie http Post请求?

时间:2020-08-04 14:13:10

标签: c# http-post

我正在创建将图像发送到Web服务器的应用程序,而我的代码存在一些问题: 这是网站上的示例:

    Get your API key from your account settings page. Each user is given a unique authentication token, we call it API key. It's a 32-characters string that looks like:2abc234de56fab7c89012d34e56f6789
This key will be used for all your requests to our server.
Submit a HTTP POST request to our API URL: http://azcaptcha.com/in.php with parameters corresponding to the type of your captcha.
Server will return captcha ID or an error code if something went wrong.
Make a timeout: 20 seconds for ReCaptcha, 5 seconds for other types of captchas.
Submit a HTTP GET request to our API URL: http://azcaptcha.com/res.php to get the result.
If captha is already solved server will return the answer in format corresponding to the type of your captcha.
By default answers are returned as plain text like: OK|Your answer. But answer can also be returned as JSON {"status":1,"request":"TEXT"} if json parameter is used.
If captcha is not solved yet server will return CAPCHA_NOT_READY result. Repeat your request in 5 seconds.
If something went wrong server will return an error code.
          

这是Base64示例表单

<form method="post" action="http://azcaptcha.com/in.php">
<input type="hidden" name="method" value="base64">
Your key:
<input type="text" name="key" value="YOUR_APIKEY">
The CAPTCHA file body in base64 format:
<textarea name="body">BASE64_FILE</textarea>
<input type="submit" value="Upload and get the ID">
</form> 

但是当我尝试发送该消息时,我从服务器收到错误消息,指出无法读取base64文件,但是如果我通过HTML页面使用base64示例中的代码发送该消息,它将起作用,这就是我的C#代码:

byte[] imageArray = System.IO.File.ReadAllBytes(@"C:\Users\Cvetan Tokov\Desktop\New folder\rep.png");
            string base64ImageRepresentation = Convert.ToBase64String(imageArray);
            richTextBox1.Text = base64ImageRepresentation;
           
             string key = "MY API KEY";
             ASCIIEncoding encoding = new ASCIIEncoding();
             string postData = richTextBox1.Text;

             byte[] data = encoding.GetBytes(postData);
             WebRequest request = WebRequest.Create("http://azcaptcha.com/in.php?key=MY API KEY&method=base64");
             request.Method = "post";
            
             request.ContentType = "text/html; charset=windows-1252";

            request.ContentLength = data.Length;

             using (Stream stream = request.GetRequestStream())
             {
                 stream.Write(data, 0, data.Length);
             }

此代码未按正确的顺序发送base64字符串以从服务器读取 有人可以帮我从C#中的示例创建表单吗。

先谢谢您

1 个答案:

答案 0 :(得分:0)

我建议您使用网络嗅探器。例如,HTTP分析器。启用它,通过浏览器和您的应用发送请求,然后比较有什么区别-主要参见“标题”和“发布数据”标签。