c# 代码仅供参考如有错误请联系客户,会提供技术支持
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Net;
public static string httpget(string url)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "Get";
//req.Headers.Add()
try
{
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
public static string uploadimg(string imagePath)
{
// 图片文件的本地路径,替换为实际要上传的图片路径
//
// 上传图片的服务器接口地址,替换为真实有效的地址
string uploadUrl = "http://dt1.hyocr.com:8080/uploadpic.php";
string responseText = string.Empty;
// 模拟多个请求参数,可根据实际情况替换具体值
Dictionary parameters = new Dictionary()
{
{ "dati_type", "8091" }, //答题类型
{ "acc_str", "xxxxxxxxxxxx" },//用户自己的答题串 以便计费 登录账号查询自己的答题密码串
{ "extra_str", "题目是。。。。" } ,//描述问题内容,以便答题人员能够理解答题
{ "zz", "" } ,//作者帐号(给予返利)
{ "pri", "9" } ,//优先级
{ "timeout", "70" } ,//答题时间不要太小,否则容易超时而不能返回结果
};
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
byte[] requestBody = BuildRequestBody(imagePath, parameters);
request.ContentLength = requestBody.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(requestBody, 0, requestBody.Length);
}
try
{
using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
responseText = reader.ReadToEnd();
Console.WriteLine("服务器返回内容: " + responseText);
}
}
}
catch (WebException ex)
{
using (WebResponse errorResponse = ex.Response)
{
if (errorResponse!= null)
{
using (StreamReader reader = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8))
{
responseText = reader.ReadToEnd();
Console.WriteLine("请求出错,服务器返回错误信息: " + errorText);
}
}
}
}
return responseText;
}
static byte[] BuildRequestBody(string imagePath, Dictionary parameters)
{
byte[] boundaryBytes = Encoding.ASCII.GetBytes("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
byte[] endBoundaryBytes = Encoding.ASCII.GetBytes("\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n");
MemoryStream memoryStream = new MemoryStream();
foreach (var param in parameters)
{
memoryStream.Write(boundaryBytes, 0, boundaryBytes.Length);
string paramHeader = $"Content-Disposition: form-data; name=\"{param.Key}\"\r\n\r\n{param.Value}\r\n";
byte[] paramHeaderBytes = Encoding.UTF8.GetBytes(paramHeader);
memoryStream.Write(paramHeaderBytes, 0, paramHeaderBytes.Length);
}
using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
memoryStream.Write(boundaryBytes, 0, boundaryBytes.Length);
string imageHeader = $"Content-Disposition: form-data; name=\"pic\"; filename=\"{Path.GetFileName(imagePath)}\"\r\nContent-Type: image/jpeg\r\n\r\n";
byte[] imageHeaderBytes = Encoding.UTF8.GetBytes(imageHeader);
memoryStream.Write(imageHeaderBytes, 0, imageHeaderBytes.Length);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytesRead);
}
}
memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
return memoryStream.ToArray();
}
public static void main(){
string imagePath = @"C:\temp\test.jpg";
string result = uploadimg(imagePath);
if(result.Substring(0,1)=="#"){
Console.WriteLine("出错了 : " + responseText);
return;
}
string sid = result;
string getAnswerUrl = "http://dt1.hyocr.com:8080/Query.php?sid=" + sid;
for (int i = 0; i < 60;i++ )
{
string answer_tmp = httpget(getAnswerUrl);
if(answer_tmp!=""){
if(answer_tmp.Substring(0,1)=="#"){
Console.WriteLine("识别出错了: " + answer_tmp);
}else{
Console.WriteLine("识别结果是: " + answer_tmp);
}
break;
}
Thread.Sleep(1000);
}
}
说明:xxxxxxxxxxxxxxx替换成密码串 提交成功后会返回一组字符串 直接用这个字符串每1秒循环get提交到http://dt1.hyocr.com:8080/Query.php 即可 一直取到答案为止 取到答案判断 一下答案的第一个字符串 如果为“#” 即为报错了 如果不是#开头 即为返回的答案