欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > c# httppost请求接口传参数及图片上传,非常实用

c# httppost请求接口传参数及图片上传,非常实用

2024/11/30 2:57:19 来源:https://blog.csdn.net/leidong2009/article/details/144112986  浏览:    关键词:c# httppost请求接口传参数及图片上传,非常实用

1、封装HttpPost类库,包括参数传递和文件流上传;

public static string[] HttpPost(string url, string filepath, string tokenid,string userid, string version, string devicetype, string filecontents, string filename)
{string[] strarr = new string[2];string strResult;try{FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);byte[] bytebuffer;bytebuffer = new byte[fs.Length];fs.Read(bytebuffer, 0, (int)fs.Length);fs.Close();string boundary = DateTime.Now.Ticks.ToString("X");string Enter = "\r\n";string strContent1 = "--" + boundary + Enter + "Content-Type: application/octet-stream" + Enter + "Content-Disposition: form-data; filename=\"" + "" + filename + "" + "\"; name=\"filedata\"" + Enter + Enter;string strContent2 = Enter + "--" + boundary + Enter + "Content-Type: text/plain" + Enter + "Content-Disposition: form-data; name=\"tokenid\"" + Enter + Enter + "" + tokenid + "";string strContent3 = Enter + "--" + boundary + Enter + "Content-Type: text/plain" + Enter + "Content-Disposition: form-data; name=\"userid\"" + Enter + Enter + "" + userid + "";string strContent4 = Enter + "--" + boundary + Enter + "Content-Type: text/plain" + Enter + "Content-Disposition: form-data; name=\"version\"" + Enter + Enter + "" + version + "";string strContent5 = Enter + "--" + boundary + Enter + "Content-Type: text/plain" + Enter + "Content-Disposition: form-data; name=\"devicetype\"" + Enter + Enter + "" + devicetype + "";string strContent6 = Enter + "--" + boundary + Enter + "Content-Type: text/plain" + Enter + "Content-Disposition: form-data; name=\"filecontents\"" + Enter + Enter + "" + filecontents + "";string strContent7 = Enter + "--" + boundary + Enter + "Content-Type: text/plain" + Enter + "Content-Disposition: form-data; name=\"filename\"" + Enter + Enter + "" + filename + "" + Enter + "--" + boundary + "--";byte[] strContentByte1 = Encoding.UTF8.GetBytes(strContent1);byte[] strContentByte2 = Encoding.UTF8.GetBytes(strContent2);byte[] strContentByte3 = Encoding.UTF8.GetBytes(strContent3);byte[] strContentByte4 = Encoding.UTF8.GetBytes(strContent4);byte[] strContentByte5 = Encoding.UTF8.GetBytes(strContent5);byte[] strContentByte6 = Encoding.UTF8.GetBytes(strContent6);byte[] strContentByte7 = Encoding.UTF8.GetBytes(strContent7);HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.Method = "POST";request.ContentType = "multipart/form-data;boundary=" + boundary;Stream myRequestStream = request.GetRequestStream();myRequestStream.Write(strContentByte1, 0, strContentByte1.Length);myRequestStream.Write(bytebuffer, 0, bytebuffer.Length);myRequestStream.Write(strContentByte2, 0, strContentByte2.Length);myRequestStream.Write(strContentByte3, 0, strContentByte3.Length);myRequestStream.Write(strContentByte4, 0, strContentByte4.Length);myRequestStream.Write(strContentByte5, 0, strContentByte5.Length);myRequestStream.Write(strContentByte6, 0, strContentByte6.Length);myRequestStream.Write(strContentByte7, 0, strContentByte7.Length);HttpWebResponse response = (HttpWebResponse)request.GetResponse();Stream streamReceive = response.GetResponseStream();Encoding encoding = Encoding.UTF8;StreamReader streamReader = new StreamReader(streamReceive, encoding);strResult = streamReader.ReadToEnd();streamReceive.Dispose();streamReader.Dispose();return strarr;}catch (Exception e){strarr[0] = "false";strarr[1] = e.ToString();//ErrHandler.WriteError("RequestSend-Post方法请求", "请求方法:Post方法异常:请求地址:" + strUrl + ".请求参数:" + strRequestData + "。请求类型:" + contentType + "。详细:" + e.ToString());return strarr; ;}
}

2、最重要的就是如何调用了;         

string tokenid = "111111111"; //变量参数
string userid = ”5“; //变量参数
string version = "8.0.0"; //变量参数
string devicetype = "3"; //变量参数
string filecontents = "1234"; //变量参数
string filename = "文件名";//变量参数
string[] responseStr = HttpHelp.HttpPost(url, path,  tokenid, userid,  version,  devicetype, filecontents,  filename);

path 是获取的文件路径,下面是获取上传文件的路径方法;

string path = "";
if (file != null && file.ContentLength > 0)
{
        // 获取文件名
        string fileName = Path.GetFileName(file.FileName);
        // 保存文件到服务器的某个路径
        path = Path.Combine(Server.MapPath("~/UpLoadFiles/tempfile"), fileName);
        file.SaveAs(path);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com