I'm working on the project that need to upload the file using HTTPWebRequest on the client side. When I'm using "PUT" method on the HTTPWebRequest, I'm able to retrieve the file but it's not working with "POST" method. Could someone help me with this problem?
Thanks.
Server side Request Handler code:
string tokenId = "";
Stream fin = null;
try
{
tokenSignature = false;
//To capture the tokenId
string MainString = Request.Headers.ToString();
int FirstChr = MainString.IndexOf("*=");
MainString = MainString.Substring(FirstChr + 2);
int secondChr = MainString.IndexOf("%");
tokenId = MainString.Substring(0, secondChr);
//to Write the received encrypted token into temporary folder
FileStream fs = new FileStream(AppsConfig.temp + tokenId, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
//Convert the listenerRequest into InputStream to write the token
Stream InputStream = Request..InputStream;
byte[] inData = new byte[32768];
int bytesRead;
//close the connection that is used to write the token
bw.Close();
fs.Close();
//Read the temporary encrypted token (for decryption purposes)
fin = File.OpenRead(AppsConfig.temp + tokenId);
//To read the private key
Stream prSignKey = File.OpenRead(AppsConfig.privateKey);
PgpSecretKey pgpSec;
PgpSecretKeyRingBundle ringBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(prSignKey));
//Get the company key Id and passphrase
String[] getText = new String[2];
int no = 0;
TextReader readFile = new StreamReader(AppsConfig.keyFile);
do
{
getText[no] = readFile.ReadLine();
no++;
} while (no < 2);
readFile.Close();
long KeyId = Int64.Parse(getText[0]);
Char[] passwd = getText[1].ToCharArray();
//Get the private key
pgpSec = ringBundle.GetSecretKey(KeyId);
PgpPrivateKey pgpPrivate = pgpSec.ExtractPrivateKey(passwd);
//Close all unnecessary connections
InputStream.Close();
prSignKey.Close();
readFile.Close();
Client Side upload File :
//Create a web request to the url containing the image
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
wReq.CookieContainer = cc;
wReq.Headers.Add("ResourceAccess*:" + tokenId + ":");
AccessToken combinedToken = getObject();
// Create a file stream to store the combined token temporary
using (FileStream fileStream = new FileStream(ClientConfig.temp + tokenId + ".bin",
FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
// Create a formatter.
IFormatter formatter = new BinaryFormatter();
There should be no difference between a POST and a PUT as far as ASP.NET is concerned. It's only different headers, but requests for PUT and POST are handled identically so it's likely that there's something else happening that's causing a problem.
Couple of things to check:
* Set the Post buffer limit. It might be that that is applied only to POST operations but bypassed with PUT
* Make sure that POST is enabled in the scriptmap extension (.ASPX or other handler/module) Extension
However in both of those cases you should get downright server errors if rather than a 0 byte file on the server.
bluesheeva
Member
9 Points
11 Posts
File size 0 in server when receiving HTTPWebRequest
Aug 25, 2011 03:57 AM|LINK
Hi,
I'm working on the project that need to upload the file using HTTPWebRequest on the client side. When I'm using "PUT" method on the HTTPWebRequest, I'm able to retrieve the file but it's not working with "POST" method. Could someone help me with this problem? Thanks.
Server side Request Handler code:
string tokenId = "";
Stream fin = null;
try
{
tokenSignature = false;
//To capture the tokenId
string MainString = Request.Headers.ToString();
int FirstChr = MainString.IndexOf("*=");
MainString = MainString.Substring(FirstChr + 2);
int secondChr = MainString.IndexOf("%");
tokenId = MainString.Substring(0, secondChr);
//to Write the received encrypted token into temporary folder
FileStream fs = new FileStream(AppsConfig.temp + tokenId, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
//Convert the listenerRequest into InputStream to write the token
Stream InputStream = Request..InputStream;
byte[] inData = new byte[32768];
int bytesRead;
while ((bytesRead = InputStream.Read(inData, 0, inData.Length)) > 0)
{
bw.Write(inData, 0, bytesRead);
}
//close the connection that is used to write the token
bw.Close();
fs.Close();
//Read the temporary encrypted token (for decryption purposes)
fin = File.OpenRead(AppsConfig.temp + tokenId);
//To read the private key
Stream prSignKey = File.OpenRead(AppsConfig.privateKey);
PgpSecretKey pgpSec;
PgpSecretKeyRingBundle ringBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(prSignKey));
//Get the company key Id and passphrase
String[] getText = new String[2];
int no = 0;
TextReader readFile = new StreamReader(AppsConfig.keyFile);
do
{
getText[no] = readFile.ReadLine();
no++;
} while (no < 2);
readFile.Close();
long KeyId = Int64.Parse(getText[0]);
Char[] passwd = getText[1].ToCharArray();
//Get the private key
pgpSec = ringBundle.GetSecretKey(KeyId);
PgpPrivateKey pgpPrivate = pgpSec.ExtractPrivateKey(passwd);
//Close all unnecessary connections
InputStream.Close();
prSignKey.Close();
readFile.Close();
Client Side upload File :
//Create a web request to the url containing the image
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
wReq.CookieContainer = cc;
wReq.Headers.Add("ResourceAccess*:" + tokenId + ":");
AccessToken combinedToken = getObject();
// Create a file stream to store the combined token temporary
using (FileStream fileStream = new FileStream(ClientConfig.temp + tokenId + ".bin",
FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
// Create a formatter.
IFormatter formatter = new BinaryFormatter();
// Serialize.
formatter.Serialize(fileStream, combinedToken);
}
//Perform encryption and signing
encryptToken(combinedToken);
//Create filestream to read the file from hardrive
reader = new FileStream(ClientConfig.temp + tokenId + ".asc", FileMode.Open);
wReq.Method = "POST";
wReq.ContentLength = reader.Length;
wReq.Timeout = 50000;
wReq.ContentType = "application/x-www-form-urlencoded";
//wReq.AllowWriteStreamBuffering = true;
//Create network stream to send file over http
Stream reqStream = wReq.GetRequestStream();
byte[] inData = new byte[32768];
int bytesRead = reader.Read(inData, 0, inData.Length);
//Sending file over http
while (bytesRead > 0)
{
reqStream.Write(inData, 0, bytesRead);
bytesRead = reader.Read(inData, 0, inData.Length);
}
//Close the request Stream
reqStream.Flush();
reqStream.Close();
reader.Close();
wReq.BeginGetResponse(new AsyncCallback(ReadCallBack), wReq);
asp.net
rstrahl
Contributor
2233 Points
375 Posts
ASPInsiders
MVP
Re: File size 0 in server when receiving HTTPWebRequest
Aug 26, 2011 11:37 PM|LINK
There should be no difference between a POST and a PUT as far as ASP.NET is concerned. It's only different headers, but requests for PUT and POST are handled identically so it's likely that there's something else happening that's causing a problem.
Couple of things to check:
* Set the Post buffer limit. It might be that that is applied only to POST operations but bypassed with PUT
* Make sure that POST is enabled in the scriptmap extension (.ASPX or other handler/module) Extension
However in both of those cases you should get downright server errors if rather than a 0 byte file on the server.
+++ Rick ---
West Wind Technologies
Making waves on the Web
www.west-wind.com/weblog