自学内容网 自学内容网

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);
}


原文地址:https://blog.csdn.net/leidong2009/article/details/144112986

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!