注册 登录
编程论坛 VFP论坛

哪位大神帮我把C#语言转换为VFP9.0?在此拜谢!

wcx666 发布于 2024-08-19 09:12, 219 次点击
string url = report_addr +$"thirdApi/saveHealthExamPile":
NameValueCollection nvc = new NameValueCollection ();
nve.Add("param,“{\“idNo\ :\  + sfzh + “\’,\"examDate\":\"" + tjb. Jdrq. Value. ToString("yyyy-M-dd) + "\, \"type\":\"5\}"):
var task = uploadGXTJ(lt.nvc,ms. ToArray(),url,partner_license):


async
Task<qmResult>
uploadGXTJ
LoginToken
lt.
NameValueCollection formData,
byte[]
file.
string url,
string partner_license)
try
using(HttpClient webClient = new HttpClient())
// 添加表单数据
//webClient.Headers.Add("Content-Type","multipart/form-data");
webClient. DefaultRequestHeaders.AcceptEncoding. Add(new StringWithQualityHeaderValue("utf-8"));
webClient. DefaultRequestHeaders.Add("access_token", lt.access_token);
webClient. DefaultRequestHeaders.Add("partner-license", partner_license);
using (var mformData = new MultipartFormDataContent ()
var fileContent = new StreamContent(new MemoryStream(file));
fileContent.Headers.ContentType = MediaTypeHeaderValue. Parse("multipart/form-data");
mformData.Add(fileContent,“file", Path.GetFileName("mn.jpg"));
foreach (var key in formData.AllKeys)
var keyValueContent = new StringContent(formData[key]):
mformData.Add (keyValueContent, key): // 替换为你的表单键名
2 回复
#2
wcx6662024-08-19 09:19
主要是以下 {"param":[{"idNo":"152701195712084815","examDate":"2022-01-06","type":"1"}],
         "file":图像的字符串}
 请教一下这个写法对吗?写两行怎样结合的?报文内容怎样处理?第二行应该写还是不应该写?如果写用什么格式?
如果合适请您给写一下。拜托
#3
yiyanxiyin2024-08-19 11:18
可以使用socket, 逻辑更简单一些, http post主要就是header和body, header必须为multipart/form-data才能同时post表单域和图片,  参考代码(python的, 但http协议都一样, 转成vpf很简单,全是拼接的字符串):
import socket
import os
 
# 图片路径和文件名
image_path = 'path/to/your/image.jpg'
 
# 服务器地址和端口
HOST, PORT = ", 80
 
# 读取图片内容
with open(image_path, 'rb') as f:
    image_data = f.read()
 
# 图片文件名
image_filename = os.path.basename(image_path)
 
# 创建 socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
 
# 构建请求头
boundary = '----------ThIsIsABoundary'
end_boundary = '--' + boundary + '--\r\n'
body = []
 
# 添加表单域数据
body.append('--' + boundary)
body.append('Content-Disposition: form-data; name="fieldname"')
body.append('')
body.append('value')
 
# 添加图片数据
body.append('--' + boundary)
body.append('Content-Disposition: form-data; name="image"; filename="{0}"'.format(image_filename))
body.append('Content-Type: image/jpeg')
body.append('')
body.append(image_data)
 
# 最后添加结束边界
body.append(end_boundary)
 
# 构建请求体
body = '\r\n'.join(body)
 
# 设置请求头
header = 'POST /upload HTTP/1.1\r\n'
header += 'Host: {0}:{1}\r\n'.format(HOST, PORT)
header += 'Content-Type: multipart/form-data; boundary={0}\r\n'.format(boundary)
header += 'Content-Length: {0}\r\n\r\n'.format(len(body))
 
# 发送请求
s.send(header.encode('utf-8'))
s.send(body.encode('utf-8'))
 
# 接收响应
response = s.recv(1024)
print(response)
 
# 关闭连接
s.close()

[此贴子已经被作者于2024-8-19 11:20编辑过]

1