HttpWebRequest TimeOut On an Https channel while posting data

G

Guest

Hello,

When I'm uploading an file to a JRun WebServer (third party) with a Windows
Forms application, l always get an TimeOut while uploading, all other request
who doesn't request an post are going ok? Why? I've tried almost everything.
I also tried the KB888528 solution but this also doesn't work.

What can the KB887563 do for me?

Machine( tried on Windows 2003 Server & WinXp)
Microsoft .NET Framework 1.1 SP1

I'll hope somebody could help me because this is very frustrating.

Thanx in advance.

Greetz,

Joey Chömpff


My Code:

private void Initialize()
{
// Create a new request to the mentioned URL.
_request = (HttpWebRequest) WebRequest.Create(_url);

_request.Headers.Set("Pragma", "no-cache");
_request.UserAgent = "Sample";
_request.Timeout = 30000;
_request.Method = "GET";
_request.ContentType="text/xml";
_request.AllowAutoRedirect = true;
_request.KeepAlive = false;
_request.CookieContainer = _cookieJar;
_request.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.CertificatePolicy = new EBatchSecurityPolicy();

if (UseProxy)
{
_proxy = (WebProxy) _request.Proxy;
_proxy.Credentials = new NetworkCredential(_proxyUserName, _proxyPassword);
_request.Proxy=_proxy;
}
}

private void AddPostData()
{
string boundary = "---------------------------" +
DateTime.Now.Ticks.ToString("x");
_request.ContentType = "multipart/form-data; boundary=" + boundary;
_request.Method = "POST";

// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(_fileNameToPost);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");

string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.Default.GetBytes(postHeader);

// Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes =
Encoding.Default.GetBytes(string.Format("\r\n--{0}--\r\n",boundary));

using(MemoryStream stream = new MemoryStream())
{
// Write out our post header
stream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

// Write out the file contents
byte[] buffer = new Byte[checked((uint) Math.Min(4096, (int)
_dataToPost.Length))];
int bytesRead = 0;
int totalBytesRead = 0;
while ( (bytesRead = _dataToPost.Read(buffer, 0, buffer.Length)) != 0 )
{
stream.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}

// Write out the trailing boundary
stream.Write(boundaryBytes, 0, boundaryBytes.Length);

long length = postHeaderBytes.Length + totalBytesRead +
boundaryBytes.Length;
_request.ContentLength = length;

using(Stream requestStream = _request.GetRequestStream())
{
stream.WriteTo(requestStream);
requestStream.Close();
}

stream.Close();
}
}

public ResponseBase GetResponse(Type responseType)
{
if (_dataToPost != null && _dataToPost.Length > 0)
AddPostData();
// Read the response
using(HttpWebResponse webResponse = (HttpWebResponse)_request.GetResponse())
{
ResponseBase response = null;
using (StreamReader sr = new
StreamReader(webResponse.GetResponseStream(), System.Text.UTF8Encoding.UTF8))
{
string responseString = sr.ReadToEnd();

using(StringReader stringReader = new StringReader(responseString))
{
response = (ResponseBase) Serializer.Read(responseType, stringReader);
stringReader.Close();
}
sr.Close();
}

webResponse.Close();
return response;
}
}

My Exception:

Message: The operation has timed-out. StackTrace: at
System.Net.HttpWebRequest.GetResponse()
at ECH.EBatch.Common.Communication.EBatchCommunicator.GetResponse(Type
responseType)
at ECH.EBatch.Common.EBatchManager.GetResponse(EBatchService service,
String url, Type responseType, Stream dataToPost, String fileName)
at ECH.EBatch.Common.EBatchManager.UploadFile(String fileName,
EBatchService ebatchService)
at
ECH.EBatch.TestApplication.EBatchManagerTest.UploadOldFiles(EBatchService
service)
at ECH.EBatch.TestApplication.frmMain.btnUpload_Click(Object sender,
EventArgs e)
 
G

Guest

I've seen that I didn't use the same encoding everywhere, but this wasnt the
solution.

I've tried to search my own post and then I saw an post from "Haacked" he
noticed something about "the Expect-100 problem". He had an url to his
WebLog who gave me a solotion of my problem
(http://haacked.com/archive/2004/05/15/449.aspx).

Thanx.

Joey Chömpff said:
Hello,

When I'm uploading an file to a JRun WebServer (third party) with a Windows
Forms application, l always get an TimeOut while uploading, all other request
who doesn't request an post are going ok? Why? I've tried almost everything.
I also tried the KB888528 solution but this also doesn't work.

What can the KB887563 do for me?

Machine( tried on Windows 2003 Server & WinXp)
Microsoft .NET Framework 1.1 SP1

I'll hope somebody could help me because this is very frustrating.

Thanx in advance.

Greetz,

Joey Chömpff


My Code:

private void Initialize()
{
// Create a new request to the mentioned URL.
_request = (HttpWebRequest) WebRequest.Create(_url);

_request.Headers.Set("Pragma", "no-cache");
_request.UserAgent = "Sample";
_request.Timeout = 30000;
_request.Method = "GET";
_request.ContentType="text/xml";
_request.AllowAutoRedirect = true;
_request.KeepAlive = false;
_request.CookieContainer = _cookieJar;
_request.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.CertificatePolicy = new EBatchSecurityPolicy();

if (UseProxy)
{
_proxy = (WebProxy) _request.Proxy;
_proxy.Credentials = new NetworkCredential(_proxyUserName, _proxyPassword);
_request.Proxy=_proxy;
}
}

private void AddPostData()
{
string boundary = "---------------------------" +
DateTime.Now.Ticks.ToString("x");
_request.ContentType = "multipart/form-data; boundary=" + boundary;
_request.Method = "POST";

// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("file");
sb.Append("\"; filename=\"");
sb.Append(_fileNameToPost);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");

string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.Default.GetBytes(postHeader);

// Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes =
Encoding.Default.GetBytes(string.Format("\r\n--{0}--\r\n",boundary));

using(MemoryStream stream = new MemoryStream())
{
// Write out our post header
stream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

// Write out the file contents
byte[] buffer = new Byte[checked((uint) Math.Min(4096, (int)
_dataToPost.Length))];
int bytesRead = 0;
int totalBytesRead = 0;
while ( (bytesRead = _dataToPost.Read(buffer, 0, buffer.Length)) != 0 )
{
stream.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}

// Write out the trailing boundary
stream.Write(boundaryBytes, 0, boundaryBytes.Length);

long length = postHeaderBytes.Length + totalBytesRead +
boundaryBytes.Length;
_request.ContentLength = length;

using(Stream requestStream = _request.GetRequestStream())
{
stream.WriteTo(requestStream);
requestStream.Close();
}

stream.Close();
}
}

public ResponseBase GetResponse(Type responseType)
{
if (_dataToPost != null && _dataToPost.Length > 0)
AddPostData();
// Read the response
using(HttpWebResponse webResponse = (HttpWebResponse)_request.GetResponse())
{
ResponseBase response = null;
using (StreamReader sr = new
StreamReader(webResponse.GetResponseStream(), System.Text.UTF8Encoding.UTF8))
{
string responseString = sr.ReadToEnd();

using(StringReader stringReader = new StringReader(responseString))
{
response = (ResponseBase) Serializer.Read(responseType, stringReader);
stringReader.Close();
}
sr.Close();
}

webResponse.Close();
return response;
}
}

My Exception:

Message: The operation has timed-out. StackTrace: at
System.Net.HttpWebRequest.GetResponse()
at ECH.EBatch.Common.Communication.EBatchCommunicator.GetResponse(Type
responseType)
at ECH.EBatch.Common.EBatchManager.GetResponse(EBatchService service,
String url, Type responseType, Stream dataToPost, String fileName)
at ECH.EBatch.Common.EBatchManager.UploadFile(String fileName,
EBatchService ebatchService)
at
ECH.EBatch.TestApplication.EBatchManagerTest.UploadOldFiles(EBatchService
service)
at ECH.EBatch.TestApplication.frmMain.btnUpload_Click(Object sender,
EventArgs e)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top