|
@@ -64,7 +64,10 @@ namespace PDF_Master.Helper
|
|
|
/// </summary>
|
|
|
private static string Uri_Rewrite = host + "/api/rewrite";
|
|
|
|
|
|
-
|
|
|
+ /// <summary>
|
|
|
+ /// chatGPT错误码
|
|
|
+ /// </summary>
|
|
|
+ public static string ChatGPTCode = "";
|
|
|
#region AI服务器对接接口
|
|
|
/// <summary>
|
|
|
/// 获取
|
|
@@ -369,7 +372,7 @@ namespace PDF_Master.Helper
|
|
|
request.ContentType = "application/json";
|
|
|
//request.Accept = "application/vnd.api+json;version=1";
|
|
|
request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
|
|
|
- request.Timeout = 20000;
|
|
|
+ request.Timeout = 60000;
|
|
|
request.ServicePoint.Expect100Continue = false;
|
|
|
StringWriter sw = new StringWriter();
|
|
|
using (JsonWriter writer = new JsonTextWriter(sw))
|
|
@@ -447,23 +450,36 @@ namespace PDF_Master.Helper
|
|
|
/// <returns></returns>
|
|
|
public async static Task<string> Correction(string content)
|
|
|
{
|
|
|
+
|
|
|
intProcess(true, "Correcting...");
|
|
|
- System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
App.mainWindowViewModel.Value = 1;
|
|
|
- //需要纠错的内容
|
|
|
- namevalue["project_id"] = "2";
|
|
|
- namevalue["version"] = "1.0.1";
|
|
|
+ ServicePointManager.DefaultConnectionLimit = 200;
|
|
|
+ HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_Correction);
|
|
|
+ request.Method = "Post";
|
|
|
+ //request.Accept = "application/vnd.api+json;version=1";
|
|
|
+ request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
|
|
|
+ request.Timeout = 60000;
|
|
|
+ request.ServicePoint.Expect100Continue = false;
|
|
|
+ request.KeepAlive = false;
|
|
|
+ var formData = new MultipartFormDataContent();
|
|
|
+ formData.Add(new StringContent("2"), "project_id");
|
|
|
+ formData.Add(new StringContent("1.0.1"), "version");
|
|
|
+
|
|
|
if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
|
|
|
{
|
|
|
- namevalue["user_id"] = Settings.Default.UserDate.id;
|
|
|
+ formData.Add(new StringContent(Settings.Default.UserDate.id), "user_id");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- namevalue["user_id"] = "1";
|
|
|
+ formData.Add(new StringContent("1"), "user_id");
|
|
|
}
|
|
|
- namevalue["content"] = content;
|
|
|
+ formData.Add(new StringContent(content), "content");
|
|
|
+ // 设置请求体格式
|
|
|
+ request.ContentType = formData.Headers.ContentType.ToString();
|
|
|
+ request.ContentLength = formData.Headers.ContentLength.Value;
|
|
|
+
|
|
|
App.mainWindowViewModel.Value = 3;
|
|
|
- return await PostString(Uri_Correction, namevalue, "content");
|
|
|
+ return await PostStringHttpWeb(formData, request, "content");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -473,23 +489,36 @@ namespace PDF_Master.Helper
|
|
|
/// <returns></returns>
|
|
|
public async static Task<string> Rewrite(string content)
|
|
|
{
|
|
|
+
|
|
|
intProcess(true, "Rewriting...");
|
|
|
- System.Collections.Specialized.NameValueCollection namevalue = new System.Collections.Specialized.NameValueCollection();
|
|
|
App.mainWindowViewModel.Value = 1;
|
|
|
- //需要纠错的内容
|
|
|
- namevalue["project_id"] = "2";
|
|
|
- namevalue["version"] = "1.0.1";
|
|
|
+ ServicePointManager.DefaultConnectionLimit = 200;
|
|
|
+ HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_Correction);
|
|
|
+ request.Method = "Post";
|
|
|
+ //request.Accept = "application/vnd.api+json;version=1";
|
|
|
+ request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
|
|
|
+ request.Timeout = 20000;
|
|
|
+ request.ServicePoint.Expect100Continue = false;
|
|
|
+ request.KeepAlive = false;
|
|
|
+ var formData = new MultipartFormDataContent();
|
|
|
+ formData.Add(new StringContent("2"), "project_id");
|
|
|
+ formData.Add(new StringContent("1.0.1"), "version");
|
|
|
+
|
|
|
if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
|
|
|
{
|
|
|
- namevalue["user_id"] = Settings.Default.UserDate.id;
|
|
|
+ formData.Add(new StringContent(Settings.Default.UserDate.id), "user_id");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- namevalue["user_id"] = "1";
|
|
|
+ formData.Add(new StringContent("1"), "user_id");
|
|
|
}
|
|
|
- namevalue["content"] = content;
|
|
|
+ formData.Add(new StringContent(content), "content");
|
|
|
+ // 设置请求体格式
|
|
|
+ request.ContentType = formData.Headers.ContentType.ToString();
|
|
|
+ request.ContentLength = formData.Headers.ContentLength.Value;
|
|
|
+
|
|
|
App.mainWindowViewModel.Value = 3;
|
|
|
- return await PostString(Uri_Rewrite, namevalue, "content");
|
|
|
+ return await PostStringHttpWeb(formData, request, "content");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -519,17 +548,85 @@ namespace PDF_Master.Helper
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// chatGPT错误码
|
|
|
+ /// 从接口获取信息,获取message对应value值
|
|
|
/// </summary>
|
|
|
- public static string ChatGPTCode = "";
|
|
|
+ /// <param name="formData">传入参数</param>
|
|
|
+ /// <param name="request">请求体</param>
|
|
|
+ /// /// <param name="key">值</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async static Task<string> PostStringHttpWeb(MultipartFormDataContent formData, HttpWebRequest request, string key = "message")
|
|
|
+ {
|
|
|
+ App.mainWindowViewModel.Value = 6;
|
|
|
+ string repsonseData = "";
|
|
|
+ using (var requestStream = request.GetRequestStream())
|
|
|
+ {
|
|
|
+ await formData.CopyToAsync(requestStream);
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ // 获取HTTP响应
|
|
|
+ HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
|
|
|
+ using (StreamReader reader = new StreamReader(response2.GetResponseStream()))
|
|
|
+ {
|
|
|
+ string responseData = reader.ReadToEnd();
|
|
|
+ Console.WriteLine(responseData);
|
|
|
+ reader.Close();
|
|
|
+ App.mainWindowViewModel.Value = 7;
|
|
|
+ JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
|
|
|
+ App.mainWindowViewModel.Value = 8;
|
|
|
+ if (response2 != null)
|
|
|
+ {
|
|
|
+ response2.Close();
|
|
|
+ }
|
|
|
+ if (request != null)
|
|
|
+ {
|
|
|
+ request.Abort();
|
|
|
+ }
|
|
|
+ string unicode = "";
|
|
|
+ // 遍历字典对象输出键值对
|
|
|
+ ChatGPTCode = jobject["code"].ToObject<string>().ToLower();
|
|
|
+ if (ChatGPTCode == "200")
|
|
|
+ {
|
|
|
+ unicode = jobject["data"][key].ToObject<string>().ToLower();
|
|
|
+ }
|
|
|
+ //将Unicode格式转换成String
|
|
|
+ //repsonseData = Regex.Unescape(unicode);
|
|
|
+ repsonseData = unicode;
|
|
|
+ clossProcess();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (WebException ex)
|
|
|
+ {
|
|
|
+ App.mainWindowViewModel.Value = 8;
|
|
|
+ int statusCode = 0;
|
|
|
+ if (ex.Response is HttpWebResponse)
|
|
|
+ {
|
|
|
+ //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
|
|
|
+ statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
|
|
|
+ }
|
|
|
+ if (statusCode == 401)
|
|
|
+ {
|
|
|
+ ChatGPTCode = "401";
|
|
|
+ }
|
|
|
+ else ChatGPTCode = "300";
|
|
|
+ clossProcess();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return repsonseData;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 从接口获取信息,获取message对应value值
|
|
|
+ /// 从接口获取信息,获取message对应value值,WebClient没有设置请求时间参数换一种方案
|
|
|
/// </summary>
|
|
|
/// <param name="url"></param>
|
|
|
/// <param name="namevalue"></param>
|
|
|
/// <returns></returns>
|
|
|
- private async static Task<string> PostString(string url, System.Collections.Specialized.NameValueCollection namevalue, string key = "message")
|
|
|
+ private async static Task<string> PostStringWebClient(string url, System.Collections.Specialized.NameValueCollection namevalue, string key = "message")
|
|
|
{
|
|
|
App.mainWindowViewModel.Value = 5;
|
|
|
string repsonseData = "";
|