C# Multipart/Related Parsing and Attachments - c#

I am getting a post of Content-type Multipart/Related with an xml body with mainly PDF file attachments.
Is there a C# assembly that will handle the parsing of a post like this, where I can pull the attachments out with the purpose of saving them.
I have it done for Multipart/PostData, but it has to be for Multipart/Related.
Thanks

When you say "post", I get the feeling you are talking about an HTTP POST request?
Normally, for multipart/related (which tends to be email related), I would recommend my MimeKit library.
You could still use my MimeKit library to parse HTTP POST requests since MIME is MIME, but you could also take a look at Microsoft.AspNetCore.WebUtilities which has a MultipartReader class that will likely work great for this.

Related

Read SOAP XML message sent as HTTP message body

I have a 3rd party that is sending SOAP XML messages, as the HTTP message body (using HTTP Post). I need to write a program to accept / process these messages.
I have a specification document which contains 2 WSDL definitions - GetTransaction and CutOff.
I am trying to use Postman to send a test message. Content-Type is set to application/xml and the body is set to raw / XML.
I am using C#, ASP.Net (4.7.2), the code is in a Class Library. I have a Controller (ApiController) with:
[HttpPost]
[ValidationAttributes.RequireHttps]
public HttpResponseMessage Service(XmlDocument reqData)
but reqData is always null. I have tried different types instead of XmlDocument.
Now if probably a good time to say that this is all new to me - I've not created a program to accept SOAP messages before.
Should I be creating a WCF Service Application (rather than a .Net Framework Class Library)?
I've tried adding the WSDL "definitions" but they don't seem to produce anything useful. E.g. there is reference to a "GetTransaction" method, but it has a parameter list of about 150 items!?
I have found quite a lot of stuff via Google but it always seems to be about sending, rather than receiving, SOAP messages.
I appreciate that I have not really included much code to look at, but I feel like I've started from the wrong place. So, any basic guidance, suggestions or links to tutorial sites would be most welcome.

Upload image from android to ASP.NET server C#

After searching in google and here, i saw the popular approach is to convert the image to byte array and then to base64 string. this part was easy, but sending it and receiving it over HTTP is harder, and I can't find an easy way to do it.
I have 2 main question which depend one another
Send with android on HTTP:
Android 6 deprecated HttpClient, so i don't want to use that.
I thought to use Volley but i cant figure out how to make it work right.
Can you please give me an example of code to transfer it in a simple and elegant way which will be easy to intercept with C#?
Receive with C# and use of web service:
I'm not sure what is the best way to implement it? Should I create a web service method? in case i should, how can i intercept the post request? Should I create a new page to handle only this part. this way i know how to handle the request.
Edit:
I managed to create a request using HttpClient, but the base64 string after converting the file made the URI too long.
any other ideas?
I found here a post for uploading files from Android to ASP.NET Web API. However the HTTPClient was used for handling HTTP request but I think you can use code as reference.
Found the simple and elegant solution i wanted!
I use loopj library, "android-async-http" for sending files with 3-4 lines.
Then i get the request to a new aspx file in my ASP.NET server, and save it using "Request.Files" object.

How to send multipart response?

I have a service whereby I need to send a multipart response with different content types. I couldn't find any examples on how to do that with Nancy.
Is it supported in Nancy? If so, can you point me to an example?
There's nothing built in to do this so you'll have to construct your own Response type and/or corresponding Processor to build the multipart response.

How to extract multipart attachments from SOAP response

I am trying to extract the multipart attachment portion of a SOAP response.
I have a project that uses a Web Reference to talk to a 3rd party web service. I'm able to successfully make requests and get valid responses back from the service, but I don't understand where if anywhere the attachment data is going. Looking through the Web Reference autogenerated code there aren't any objects that match up to the actual attachment data.
Are there any kind options that I need to set when originally consuming the 3rd party WSDL to make this work correctly?
I had hoped there would be a .NET multipart boundary parser already, but wasn't able to find one so I wrote my own.
I have done this by getting the raw HTTP response as a string, splitting on the part boundaries ------=_Part_*. Then for each part looking for Content-Transfer-Encoding: base64, and then extracting the base 64 region and using Convert.FromBase64String to convert that to a byte array, and finally saving that as a file.

Getting a file into Request.Files

I have a device that uses a custom scripting language. This language provides the capability to set http request headers then perform a post to a URL. The server that the device communicates with is running .net, and I would like the handler on the server to retrieve data from the device by simply pulling it from the Request.Files[] collection. To do this, does the device need to support multipart posting or is it possible for the uploaded content to show up in the Files collection by simply setting a number of headers then sending the data?
I'm not a linux guy, but I did a search and I found this. Does it help at all? (see section 4.3)
http://curl.haxx.se/docs/httpscripting.html
If you mean that at the server end you want the file(s) to be readable via HttpRequest.Files, then I believe so.
I think the format that this expects is based on the File Upload RFC and therefore if you follow the standard procedure for submitting a file upload then it'll work.
As to how you go about formatting the request from your device - I'm afraid I can't help on that one. Assuming you can format an Http Post, writing any headers you need and then formatting the Post body with the file content, it'll work, though.
In order for .Net to be able to read a file posted to a header it must be in the multipart posting format. Otherwise it will just be text noise in the header and .Net won't know what to do with it.

Categories

Resources