file upload to https server not working

i am sending afile to a httpsserver and i am not getting back the same results as iexplorer is, or the same page. my page has an error that its displays, which is

"Error: Cannot find bean: "UploadBean" in any scope"

i captured the stream from iexplorer and myapp and its the same thing being sent. any ideas? here is the file that i am sending using multipart-form content type

StringBuilder^ sData = gcnew StringBuilder();

sData->Append("--" + sBoundary);
sData->Append("\r\n");
sData->Append("Content-Disposition: form-data; name=\"firstTime\"");
sData->Append("\r\n\r\n");
sData->Append("F");
sData->Append("\r\n");
sData->Append("--" + sBoundary);
sData->Append("\r\n");
sData->Append("Content-Disposition: form-data; name=\"prevFileName\"");
sData->Append("\r\n\r\n");
sData->Append(sFileName);
sData->Append("\r\n");
sData->Append("--" + sBoundary);
sData->Append("\r\n");
sData->Append("Content-Disposition: form-data; name=\"theFile\"; filename=\"");
sData->Append(sFileName);
sData->Append("\"");
sData->Append("\r\n");
sData->Append("Content-Type: application/vnd.ms-excel");
sData->Append("\r\n\r\n");
sData->Append(sFileData);
sData->Append("\r\n");
sData->Append("--" + sBoundary + "--\r\n");

i am using StringBuilder to hold thedata then converting that to a String which is then converted to Bytes, then written to the stream

ostData = sData->ToString();
ByteArray = Encoding::UTF8->GetBytes(postData);
postStream = request->GetRequestStream();
postStream->Write(ByteArray, 0, postData->Length);
postStream->Close();

any ideas on what i am doing wrong here?

[2825 byte] By [AaronSulwer] at [2007-12-23]
# 1

this is the complete source, please help me find the error in my code that is giving me this UploadBean error that there site is sending back to me!

#include "stdafx.h"

using namespace System;

using namespace System::Net;

using namespace System::IO;

using namespace System::Text;

using namespace System::Security::Cryptography::X509Certificates;

using namespace System::Net::Cache;

using namespace System::Web;

String^ Boundary()

{

String^ sBoundary;// = gcnew String("");

Random^ random = gcnew Random((int)DateTime::Now.Ticks);

for(int i=0; i<=12; i++)

{

char c=0;

while(!Char::IsLetterOrDigit(c))

c = random->Next() % 96 + 32;

sBoundary += Char::ConvertFromUtf32(c);

}

return sBoundary;

}

int main(array<System::String ^> ^args)

{

String^ sPassword = gcnew String("");

String^ sFileName = gcnew String("");

if(args->Length == 0)

{

Console::WriteLine("No Command Line Arguments found");

Console::WriteLine("--pass[password] : enter your login password for DTCC");

Console::WriteLine("--filename[filename] : file to be uploaded to DTCC");

Console::WriteLine("Note: file to be uploaded must be in the same Directory as this upload utility");

return 0;

}

for(int x=0; x<args->Length; x++)

{

if(args[x]->Contains("--pass"))

{

sPassword = args[x]->Remove(0, args[x]->IndexOf('[')+1);

sPassword = sPassword->Remove(sPassword->IndexOf(']'), 1);

}

else if(args[x]->Contains("--filename"))

{

sFileName = args[x]->Remove(0, args[x]->IndexOf('[')+1);

sFileName = sFileName->Remove(sFileName->IndexOf(']'), 1);

}

else

{

Console::WriteLine("unknown argument");

return 0;

}

}

if(String::IsNullOrEmpty(sPassword) || String::IsNullOrEmpty(sFileName))

{

Console::WriteLine("missing arguments");

return 0;

}

FileStream^ fs;

StreamReader^ sr;

try

{

fs = gcnew FileStream(sFileName, FileMode::Open);

sr = gcnew StreamReader(fs, Encoding::ASCII);

}

catch(FileNotFoundException^ e)

{

Console::WriteLine ("{0}", e->Message);

return 0;

}

String^ sFileData = gcnew String(sr->ReadToEnd());

String^ sBoundary = gcnew String(Boundary());

sr->Close();

fs->Close();

X509Store^ certStore = gcnew X509Store("MY", StoreLocation::CurrentUser);

try

{

certStore->Open(OpenFlags::ReadOnly | OpenFlags::OpenExistingOnly);

}

catch(ArgumentException^ e)

{

Console::WriteLine ("{0}", e->Message);

return 0;

}

CookieCollection^ cookieCollection = gcnew CookieCollection();

Uri^ uri = gcnew Uri("https://pcwd.dtcc.com/");

//request 1

HttpWebRequest^ request = (HttpWebRequest^)WebRequest::Create("https://pcwd.dtcc.com/dsv/servlet/login?system=dsv");

request->ContentType = "application/x-www-form-urlencoded";

request->Method = "POST";

request->AutomaticDecompression = DecompressionMethods::GZip | DecompressionMethods::Deflate;

request->DefaultCachePolicy = gcnew RequestCachePolicy(RequestCacheLevel::NoCacheNoStore);

request->AllowAutoRedirect = true;

request->CookieContainer = gcnew CookieContainer();

request->AllowAutoRedirect = true;

request->ClientCertificates = certStore->Certificates;

request->KeepAlive = true;

//post 1

Stream^ postStream = request->GetRequestStream();

String^ postData = String::Concat("login_system=dsv&opid=90&password=", sPassword);

array<Byte>^ ByteArray = Encoding::UTF8->GetBytes(postData);

postStream->Write(ByteArray, 0, postData->Length);

postStream->Close();

//response 1

HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());

cookieCollection->Add(request->CookieContainer->GetCookies(uri));

response->Cookies = cookieCollection;

Stream^ streamResponse = response->GetResponseStream();

StreamReader^ streamRead = gcnew StreamReader(streamResponse);

String^ responseString = streamRead->ReadToEnd();

streamResponse->Close();

streamRead->Close();

response->Close();

//request 2

request = (HttpWebRequest^)WebRequest::Create("https://pcwd.dtcc.com/dsv/action/OTCSignon?system=dsv");

request->ContentType = "application/x-www-form-urlencoded";

request->Method = "POST";

request->AutomaticDecompression = DecompressionMethods::GZip | DecompressionMethods::Deflate;

request->DefaultCachePolicy = gcnew RequestCachePolicy(RequestCacheLevel::NoCacheNoStore);

request->AllowAutoRedirect = true;

request->CookieContainer = gcnew CookieContainer();

request->AllowAutoRedirect = true;

request->ClientCertificates = certStore->Certificates;

request->CookieContainer->Add(cookieCollection);

request->KeepAlive = true;

//post 2

postStream = request->GetRequestStream();

postData = "isFirstTime=false&isMultipleOriginators=false&testProdIndicator=P";

ByteArray = Encoding::UTF8->GetBytes(postData);

postStream->Write(ByteArray, 0, postData->Length);

postStream->Close();

//response 2

response = dynamic_cast<HttpWebResponse^>(request->GetResponse());

response->Cookies = cookieCollection;

streamResponse = response->GetResponseStream();

streamRead = gcnew StreamReader(streamResponse);

responseString = streamRead->ReadToEnd();

streamResponse->Close();

streamRead->Close();

response->Close();

//****** Not sending file correctly, or is but not receiving correct response

//request 3

request = (HttpWebRequest^)WebRequest::Create("https://pcwd.dtcc.com/dsv/action/upload");

request->Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

request->Referer = "https://pcwd.dtcc.com/dsv/action/upload";

request->Headers->Add("Accept-Language","en-us");

request->ContentType = String::Concat("multipart/form-data; boundary=", sBoundary);

request->Headers->Add("UA-CPU","x86");

request->Method = "POST";

request->AutomaticDecompression = DecompressionMethods::Deflate | DecompressionMethods::GZip;

request->UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

request->KeepAlive = true;

request->CachePolicy = gcnew RequestCachePolicy(RequestCacheLevel::NoCacheNoStore);

request->ClientCertificates = certStore->Certificates;

request->CookieContainer = gcnew CookieContainer();

request->CookieContainer->Add(cookieCollection);

request->ServicePoint->Expect100Continue = false;

request->AllowWriteStreamBuffering = true;

//post 3

//upload file

StringBuilder^ sData = gcnew StringBuilder();

sData->Append("--");

sData->Append(sBoundary);

sData->Append("\r\n");

sData->Append("Content-Disposition: form-data; name=\"firstTime\"");

sData->Append("\r\n\r\n");

sData->Append("F");

sData->Append("\r\n");

sData->Append("--");

sData->Append(sBoundary);

sData->Append("\r\n");

sData->Append("Content-Disposition: form-data; name=\"prevFileName\"");

sData->Append("\r\n\r\n");

sData->Append(sFileName);

sData->Append("\r\n");

sData->Append("--");

sData->Append(sBoundary);

sData->Append("\r\n");

sData->Append("Content-Disposition: form-data; name=\"theFile\"; filename=\"");

sData->Append(sFileName);

sData->Append("\"");

sData->Append("\r\n");

sData->Append("Content-Type: application/vnd.ms-excel");

sData->Append("\r\n\r\n");

sData->Append(sFileData);

sData->Append("\r\n");

sData->Append("--");

sData->Append(sBoundary);

sData->Append("--");

sData->Append("\r\n");

postData = sData->ToString();

ByteArray = Encoding::UTF8->GetBytes(postData);

postStream = request->GetRequestStream();

postStream->Write(ByteArray, 0, postData->Length);

postStream->Close();

//response 3

response = dynamic_cast<HttpWebResponse^>(request->GetResponse());

response->Cookies = cookieCollection;

streamResponse = response->GetResponseStream();

streamRead = gcnew StreamReader(streamResponse);

responseString = streamRead->ReadToEnd();

streamResponse->Close();

streamRead->Close();

response->Close();

//get results from the feed and write to console

//any error or confirmation messages should go here

Console::WriteLine(responseString);

return 0;

}

AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
could it be that my app doesnt have the same permissions as iexplorer does? iexplorer works fine when i do this, but my app doesnt return the same webpage as iexplorer does. i am using a Http packet capturing tool and they both appear to be sending the same data and headers. if it is a permissions problem i have no clue how to give my app the same permissions as iexplorer does
AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3

i have checked and the data being sent to the server is the same as what iexplorer is sending so i think its something to do with the headers. here are the headers that iexplorer is sending and then my apps.

****iexplorer headers and data (dat is the same viewed by a hex editor)

POST /dsv/action/upload HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer:
https://pcwd.dtcc.com/dsv/action/upload
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=7d629b235104d2
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: pcwd.dtcc.com
Content-Length: 434
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=0000FYIYQDRSLZL5M0E520J1WAI:10q2ci2q9

--7d629b235104d2
Content-Disposition: form-data; name="firstTime"

F
--7d629b235104d2
Content-Disposition: form-data; name="prevFileName"

10152882-CDT.csv
--7d629b235104d2
Content-Disposition: form-data; name="theFile"; filename="10152882-CDT.csv"
Content-Type: application/vnd.ms-excel

*

--7d629b235104d2--

****my app's headers and data

POST /dsv/action/upload HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer:
https://pcwd.dtcc.com/dsv/action/upload
Accept-Language: en-us
Content-Type: multipart/form-data; boundary=eC5G1Vo0detYwl
UA-CPU: x86
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host: pcwd.dtcc.com
Cookie: JSESSIONID=0000EPSB1R5JWKS2HBCETQIR4CI:10q2ci2q9
Cache-Control: no-store,no-cache
Pragma: no-cache
Content-Length: 434
Accept-Encoding: gzip, deflate

--eC5G1Vo0detYwl
Content-Disposition: form-data; name="firstTime"

F
--eC5G1Vo0detYwl
Content-Disposition: form-data; name="prevFileName"

10152882-CDT.csv
--eC5G1Vo0detYwl
Content-Disposition: form-data; name="theFile"; filename="10152882-CDT.csv"
Content-Type: application/vnd.ms-excel

*

--eC5G1Vo0detYwl--

AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

I suspect the reason for this error is broken session management (which confuses your Java Web application framework, because it doesn't find any objects it has previously placed in your HTTP session). Can you use Fiddler to verify that your code properly maintains the JSESSIONID cookie?

Your code recreates a new CookieContainer for each request. Actually, you should reuse the same instance for all requests. That also means you don't need to copy cookies around — CookieContainer takes care of that.

J?rgJooss-MSFT at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5

I have created new objects for these

CookieCollection^ cookieCollection = gcnew CookieCollection();

CookieContainer^ cookieContainer = gcnew CookieContainer();

in the first response I have added these lines

request->CookieContainer = cookieContainer;

cookieContainer->Add(request->CookieContainer->GetCookies(uri));

cookieCollection->Add(request->CookieContainer->GetCookies(uri));

response->Cookies = cookieCollection;

then the rest of the code has has these lines

request->CookieContainer = cookieContainer;

response->Cookies = cookieCollection;

i get the cookie after the first GetRequestStream and then pass that around to the rest of the requests and responses, is that how it should be done? i dont think it is because its still not working. could you give me some specifics on how to make the proper changes to my code?

AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 6

Create a CookieContainer before sending the first request:

CookieContainer cookieContainer = gcnew CookieContainer();

Make sure to use this CookieContainer instance with each HttpWebRequest in your web "conversation":

firstRequest->CookieContainer = cookieContainer;

...

secondRequest->CookieContainer = cookieContainer;

...

thirdRequest->CookieContainer = cookieContainer;

There's no need to copy the response's cookies to the CookieContainer manually.

J?rgJooss-MSFT at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 7
i did as you said and no changes i am getting the same error
AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 8

this is another post that i made that goes along with this posting, if it helps at all.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=607626&SiteID=1

AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 9

It would be helpful to see a complete HTTP capture of your application's conversation and compare that to a normal browser-based conversation.

Did you check whether you maintain the same JSESSIONID cookie throughout your application's conversation?

J?rgJooss-MSFT at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 10
i make three posts and two gets, the first one is a post and the second is a get, that first get is where i get the cookie (JSESSIONID) and i passing that around for any other requests that i make. i looked at all of the posts and gets that i make and the cookie (JSESSIONID) stays the same. i could send you a complete dump of the stream for each request that i make so that you can too compare and see that i think that everything is the same between my app and iexplorer. we are sending the same data, at least it appears that we are.
AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 11

Not necessary, because you answered the most important part — the session ID stays the same. Note that the same should be true for any other cookie your receive from the servlet container; a WebLogic 8.1 server for example may also send a _wl_authcookie_ cookie, which must be sent back and forth as well. But at this point, unless we're completely missing something here, I'd have a look at what happens on the server side, if that's possible.

J?rgJooss-MSFT at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 12
no server side is not possible except what its sending my app and can be captured via a http analyzer
AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 13
not sure if this matters but i do know that the server is an apache server because of the received headers
AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 14

does apache have something similar? actually i am not sure if they are using an apache webserver, the GET header show me that they are using

Server: IBM_HTTP_SERVER/1.3.19.4 Apache/1.3.20 (Unix)

Why can i do this with iexplorer but not with my app? it must be something on my end that i am not accounting for, something missing. i do not know enough about web development to even have an educated guess. any ideas on solving this are most welcome, this is most frustrating!

AaronSulwer at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified