zoukankan      html  css  js  c++  java
  • C#模拟HTTP Form请求上传文件

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Collections.Specialized;
    
    namespace TechnetSamples
    {
    class Program
    {
    static void Main(string[] args)
    {
    string URLAuth = "https://technet.rapaport.com/HTTP/Authenticate.aspx";
    WebClient webClient = new WebClient();
    
    NameValueCollection formData = new NameValueCollection();
    formData["Username"] = "myUser";
    formData["Password"] = "myPassword";
    
    byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
    string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
    webClient.Dispose();
    
    string URL = "http://technet.rapaport.com/HTTP/Upload/Upload.aspx?Method=file";
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
    System.Net.WebRequest webRequest = System.Net.WebRequest.Create(URL);
    
    webRequest.Method = "POST";
    webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
    
    string FilePath = "C:\test.csv";
    formData.Clear();
    formData["ticket"] = resultAuthTicket;
    formData["ReplaceAll"] = "false";
    
    Stream postDataStream = GetPostStream(FilePath, formData, boundary);
    
    webRequest.ContentLength = postDataStream.Length;
    Stream reqStream = webRequest.GetRequestStream();
    
    postDataStream.Position = 0;
    
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    
    while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
    {
    reqStream.Write(buffer, 0, bytesRead);
    }
    
    postDataStream.Close();
    reqStream.Close();
    
    StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
    string Result = sr.ReadToEnd();
    }
    
    private static Stream GetPostStream(string filePath, NameValueCollection formData, string boundary)
    {
    Stream postDataStream = new System.IO.MemoryStream();
    
    //adding form data
    string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
    "Content-Disposition: form-data; name="{0}";" + Environment.NewLine + Environment .NewLine + "{1}";
    
    foreach (string key in formData.Keys)
    {
    byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate,
    key, formData[key]));
    postDataStream.Write(formItemBytes, 0, formItemBytes.Length);
    }
    
    //adding file data
    FileInfo fileInfo = new FileInfo(filePath);
    
    string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
    "Content-Disposition: form-data; name="{0}"; filename="{1}"" +
    Environment.NewLine + "Content-Type: application/vnd.ms-excel" + Environment.NewLine + Environment.NewLine;
    
    byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate,
    "UploadCSVFile", fileInfo.FullName));
    
    postDataStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
    
    FileStream fileStream = fileInfo.OpenRead();
    
    byte[] buffer = new byte[1024];
    
    int bytesRead = 0;
    
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    {
    postDataStream.Write(buffer, 0, bytesRead);
    }
    
    fileStream.Close();
    
    byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
    postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    
    return postDataStream;
    }
    }
    }
  • 相关阅读:
    了解及使用IPV6
    天气预报API(六):中国气象频道、腾讯天气--“新编码”接口的测试
    天气预报API(五):城市代码--“新编码”和“旧编码” 对比
    天气预报API(三):免费接口测试(“旧编码”)
    nginx 配置反向代理和静态资源
    centos 7 安装mysql5.7
    java 重新学习 (四)
    java 重新学习 (三)
    java 重新学习 (二)
    h5唤醒手机拨打电话
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/5969850.html
Copyright © 2011-2022 走看看