how to create multipart HTTP request using HttpWebRequest

  • Thread starter Thread starter yoram.ayalon
  • Start date Start date
Y

yoram.ayalon

Hi,

I need to create a multipart request to UPS manifest upload electronic
service. UPS wants the request to consist of a series of headers and
bodies, and its not clear how can I use the HttpWebRequest object to do
this. there is the Headers collection, but I don't think it will give
me the control we need.

is there a low-level way to create the entire stream and attach to the
object before posting ?

this is the requirement for the request from UPS: notice it has the
first header, specifying Multipart and a boundry defined by the work
BOUNDRY.with a total length of 1040

then you have the first boundry, with length=140, the actual content
for this boundry, then another boundry ...

============================================
POST /hapld/tos/kdwhapltos HTTP/1.1<cr/lf>
Host: www.pld-certify.ups.com<cr/lf>
Content-type: multipart/mixed; boundary=BOUNDARY<cr/lf>
Content-length: 1040<cr/lf>
<cr/lf>
--BOUNDARY<cr/lf>
Content-type: application/x-www-form-urlencoded<cr/lf>
Content-length: 140<cr/lf>
<cr/lf>
AppVersion=1.0&AcceptUPSLicenseAgreement=Yes&ResponseType=application/x-ups-pld&VersionNumber=
V4R1&UserId=useridvalue&Password=passwordvalue<cr/lf>
<cr/lf>
--BOUNDARY<cr/lf>
Content-type: application/x-ups-binary<cr/lf>
Content-length: 719<cr/lf>
<cr/lf>
020082 2.0 2002101700000000000010500 000000001*AA0A1754 US
1234567002000001*BA1z1234560100002352 00001+0000000000000010 +000
0000000000000LBS01PRE10 3INUSD000001*CA18ATTENTION
DELIVERY 234 SOME LOCAL ST SO
ME CITY NJ 07652 US12015551212 *PA1z1234560100002352
02+0000010 +0000010
*SA000004<cr/lf>
<cr/lf>
--BOUNDARY--<cr/lf>
 
Hello,

have you tried to set the HttpWebRequest.SendChunked to true?

Best regards,
Henning Krause
 
thanks for the answer!

how would SendChunked alter the control over the request ? it seems
only relevant to allow breaking of large requests into multiple, but no
fine control over logical sections, which is what I need.

there seems to be no support for Multipart with this class. do you
think I can create only the top-level headers, and then prepare
everything else as one content, including the second "content-type" and
"content-length" sections ?
 
its actually working! I create one long string that contains all the
secondary boundries , each with its own content-type and content-length
headers. most of the complexity stems from having to calculate the
total length to put in the initial content-length header. I wonder if
this is a specific UPS requirement or part of the protocol RFC, but oh
well, it can't hurt and its working!



the trick is to add correctly the lengths of al the various parts so
the content-length
 
Back
Top