Java POST multipart/mixed in.NET

Java POST multipart/mixed in.NET,java,c#,.net,multipart,Java,C#,.net,Multipart,正如标题所说,如果有任何帮助的话,我有以下java代码(多部分由json对象和文件组成): (使用com.sun.jersey.multipart)并且我希望在.NET(C#)中创建相同的 到目前为止,我成功地发布了json对象,如下所示: Uri myUri = new Uri("http://srezWebServices/rest/ws0/test"); var httpWebRequest = (HttpWebRequest)WebRequest.Create(myUri); httpW

正如标题所说,如果有任何帮助的话,我有以下java代码(多部分由json对象和文件组成):

(使用com.sun.jersey.multipart)并且我希望在.NET(C#)中创建相同的

到目前为止,我成功地发布了json对象,如下所示:

Uri myUri = new Uri("http://srezWebServices/rest/ws0/test");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
httpWebRequest.Proxy = null;
httpWebRequest.Accept = "application/json";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
Console.Write("START!");

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())){
                string json = new JavaScriptSerializer().Serialize(new
                {
                    wsId = "0",
                    accessId = "101",
                    accessCode = "x@ds!2"
                });

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
        HttpContent stringStreamContent = new StringContent(json);
        stringStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        HttpContent fileStreamContent = new StreamContent(fileStream);
        fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        // Construct a MultiPart
        // 1st : JSON Object with IN parameters
        // 2nd : Octet Stream with file to upload
        var content = new MultipartContent("mixed");
        content.Add(stringStreamContent);
        content.Add(fileStreamContent);

        // POST the request as "multipart/mixed"
        var result = client.PostAsync(myUrl, content).Result;

但我想一起发送文件。内容类型必须是“multipart/mixed”,因为这是web服务得到的。我试图找到一些支持多部分的软件包,但除了这个(它不是免费的,所以我不能使用它)之外,我什么也没找到。

我最终设法这样做:

Uri myUri = new Uri("http://srezWebServices/rest/ws0/test");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
httpWebRequest.Proxy = null;
httpWebRequest.Accept = "application/json";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
Console.Write("START!");

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())){
                string json = new JavaScriptSerializer().Serialize(new
                {
                    wsId = "0",
                    accessId = "101",
                    accessCode = "x@ds!2"
                });

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
        HttpContent stringStreamContent = new StringContent(json);
        stringStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        HttpContent fileStreamContent = new StreamContent(fileStream);
        fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        // Construct a MultiPart
        // 1st : JSON Object with IN parameters
        // 2nd : Octet Stream with file to upload
        var content = new MultipartContent("mixed");
        content.Add(stringStreamContent);
        content.Add(fileStreamContent);

        // POST the request as "multipart/mixed"
        var result = client.PostAsync(myUrl, content).Result;

你的问题是什么?我想知道如何在.NET(最好是C#)中创建和发布多部分/混合的JAVA代码,就像我发布的JAVA代码一样。我看到我的问题已经被否决了,我想知道为什么。如果有什么不对劲,或者不清楚,请告诉我。可能是因为你的问题表明你没有尽力自己解决问题。现在就像是“我用java编写的,有人能帮我用C语言编写吗?”。您应该发布您到目前为止尝试过的内容,并告诉我们什么不起作用,什么是预期的行为,以及发生了什么。@BackSlash,我认为在.NET中创建和发布多部分很容易,但我刚刚错过了。不管怎样,我确实花了很多精力来寻找解决方案,所以我编辑了我的问题。请随时询问是否有不清楚的地方。