弹来弹去跑马灯!

c# 上传图片到一个外链相册服务器

这里一个免费上传图片的网站:https://imgbb.com

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
private void post1(string filePath)
      {
 
          try
          {
              string fName = new FileInfo(filePath).Name;
              string url = @"https://imgbb.com/json";
              string source = fName;
              string auth_token = "ff5ee7541280debc0674220f604c8441829a7f68";//auth_token 用抓包或者浏览器F12得到。
              string timestamp = "" + ConvertDateTimeToInt(DateTime.Now);
              string fileName = fName;
 
              string action = "upload";
              string type = "file";
              byte[] fileContentByte = new byte[1024]; // 文件内容二进制
              #region 将文件转成二进制
 
              FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
              fileContentByte = new byte[fs.Length]; // 二进制文件
              fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
              fs.Close();
 
              #endregion
 
 
              #region 定义请求体中的内容 并转成二进制
 
              string boundary = "WebKitFormBoundaryQpiD7Zd4EUOicN44";
              string Enter = "\r\n";
 
              string modelIdStr = "--" + boundary + Enter
                      + "Content-Disposition: form-data; name=\"source\"" + Enter + Enter
                      + source + Enter;
 
              string fileContentStr = "--" + boundary + Enter
                      + "Content-Type:image/jpeg" + Enter
                      + "Content-Disposition: form-data; name=\"source\"; filename=\"" + fileName + "\"" + Enter + Enter;
 
              string updateTimeStr = Enter + "--" + boundary + Enter
                      + "Content-Disposition: form-data; name=\"timestamp\"" + Enter + Enter
                      + timestamp;
 
              string enAction = Enter + "--" + boundary + Enter
                        + "Content-Disposition: form-data; name=\"action\"" + Enter + Enter
                        + action;
 
              string enType = Enter + "--" + boundary + Enter
                        + "Content-Disposition: form-data; name=\"type\"" + Enter + Enter
                        + type;
 
              string enauth_token = Enter + "--" + boundary + Enter
                      + "Content-Disposition: form-data; name=\"encrypt\"" + Enter + Enter
                      + auth_token + Enter + "--" + boundary + "--";
              var modelIdStrByte = Encoding.UTF8.GetBytes(modelIdStr);//modelId所有字符串二进制
 
              var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名称等信息的二进制(不包含文件本身)
 
              var updateTimeStrByte = Encoding.UTF8.GetBytes(updateTimeStr);//updateTime所有字符串二进制
              var encryptAction = Encoding.UTF8.GetBytes(enAction);//enAction所有字符串二进制
 
              var encryptType = Encoding.UTF8.GetBytes(enType);//enType所有字符串二进制
 
              var encryptStrByte = Encoding.UTF8.GetBytes(enauth_token);//encrypt所有字符串二进制
 
 
              #endregion
 
 
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
              request.Method = "POST";
              request.ContentType = "multipart/form-data;boundary=" + boundary;
 
              Stream myRequestStream = request.GetRequestStream();//定义请求流
 
              #region 将各个二进制 安顺序写入请求流 modelIdStr -> (fileContentStr + fileContent) -> uodateTimeStr -> encryptStr
 
              myRequestStream.Write(modelIdStrByte, 0, modelIdStrByte.Length);
 
              myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
              myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);
              myRequestStream.Write(updateTimeStrByte, 0, updateTimeStrByte.Length);
              myRequestStream.Write(encryptAction, 0, encryptAction.Length);
              myRequestStream.Write(encryptType, 0, encryptType.Length);
 
 
              myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);
 
              #endregion
 
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();//发送
 
              Stream myResponseStream = response.GetResponseStream();//获取返回值
              StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
              string retString = myStreamReader.ReadToEnd();
              myStreamReader.Close();
              myResponseStream.Close();
 
              MessageBox.Show(retString);
 
          }
          catch(Exception ex){
 
              MessageBox.Show(ex.Message);
          }
 
 
 
      }
 
 
 
 
 
 
 
      /// <summary> 
      /// 将c# DateTime时间格式转换为Unix时间戳格式 
      /// </summary> 
      /// <param name="time">时间</param> 
      /// <returns>long</returns> 
      public static long ConvertDateTimeToInt(System.DateTime time)
      {
          System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
          long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位     
          return t;
      }

  例子调用: post1("D:\\1.png");

posted @   wgscd  阅读(847)  评论(2编辑  收藏  举报
点击右上角即可分享
微信分享提示