从C#客户端多部分 [英] Multipart forms from C# client

查看:146
本文介绍了从C#客户端多部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图填补了形式从C#客户端(如Outlook插件)PHP应用程序。我用小提琴手看到来自PHP应用程序中的原始请求和窗体作为一个多/形式传送。不幸的是.NET没有配备原生支持这种类型的表格(Web客户端具有只能用于上载文件的方法)。
是否有人知道图书馆或有一些code来实现这一目标?我要发布不同的价值观和额外的(但只是有时)的文件。

I am trying to fill a form in a php application from a C# client (Outlook addin). I used Fiddler to see the original request from within the php application and the form is transmitted as a multipart/form. Unfortunately .Net does not come with native support for this type of forms (WebClient has only a method for uploading a file). Does anybody know a library or has some code to achieve this? I want to post different values and additionally (but only sometimes) a file.

感谢您的帮助,
塞巴斯蒂安

Thanks for your help, Sebastian

推荐答案

这是切断,从一些样本code我写贴,希望它应该给基础。它只支持目前文件数据和表格数据。

This is cut and pasted from some sample code I wrote, hopefully it should give the basics. It only supports File data and form-data at the moment.

public class PostData
{

    private List<PostDataParam> m_Params;

    public List<PostDataParam> Params
    {
    	get { return m_Params; }
    	set { m_Params = value; }
    }

    public PostData()
    {
    	m_Params = new List<PostDataParam>();

    	// Add sample param
    	m_Params.Add(new PostDataParam("email", "MyEmail", PostDataParamType.Field));
    }


    /// <summary>
    /// Returns the parameters array formatted for multi-part/form data
    /// </summary>
    /// <returns></returns>
    public string GetPostData()
    {
    	// Get boundary, default is --AaB03x
    	string boundary = ConfigurationManager.AppSettings["ContentBoundary"].ToString();

    	StringBuilder sb = new StringBuilder();
    	foreach (PostDataParam p in m_Params)
    	{
    		sb.AppendLine(boundary);

    		if (p.Type == PostDataParamType.File)
    		{
    			sb.AppendLine(string.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", p.Name, p.FileName));
    			sb.AppendLine("Content-Type: text/plain");
    			sb.AppendLine();
    			sb.AppendLine(p.Value);					
    		}
    		else
    		{
    			sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", p.Name));
    			sb.AppendLine();
    			sb.AppendLine(p.Value);
    		}
    	}

    	sb.AppendLine(boundary);

    	return sb.ToString();			
    }
}

public enum PostDataParamType
{
    Field,
    File
}

public class PostDataParam
{


    public PostDataParam(string name, string value, PostDataParamType type)
    {
    	Name = name;
    	Value = value;
    	Type = type;
    }

    public string Name;
    public string FileName;
    public string Value;
    public PostDataParamType Type;
}

要发送,那么你需要的数据:

To send the data you then need to:

HttpWebRequest oRequest = null;
oRequest = (HttpWebRequest)HttpWebRequest.Create(oURL.URL);
oRequest.ContentType = "multipart/form-data";   					
oRequest.Method = "POST";
PostData pData = new PostData();

byte[] buffer = encoding.GetBytes(pData.GetPostData());

// Set content length of our data
oRequest.ContentLength = buffer.Length;

// Dump our buffered postdata to the stream, booyah
oStream = oRequest.GetRequestStream();
oStream.Write(buffer, 0, buffer.Length);
oStream.Close();

// get the response
oResponse = (HttpWebResponse)oRequest.GetResponse();

这就是希望清晰,我剪了,并从几个来源粘贴到拿到整洁。

Hope thats clear, i've cut and pasted from a few sources to get that tidier.

这篇关于从C#客户端多部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆