Hello,
I am trying to make my FEZ Cobra communicate in SOAP with a service asmx on my PC, using SOAP 1.1.
It works fine but when I execute it several times on my FEZ, the GetRequestStream() takes between 300 ms to 4 s !!
Any idea what can make this time so unpredictable :’(and so long :o ??
Here is the code of my client. The dealing of the response is not important now :
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Socket = System.Net.Sockets.Socket;
using System.IO;
using MFUtilities;
namespace HttpClientSample
{
public static class MyHttpClient
{
static string url = "http://172.30.5.135/Services/LioService.asmx";
static string action = string.Empty;
private static string CreateSoapEnvelope()
{
// Add
String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
request += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
request += "<soap:Body>\n";
request += "<Add xmlns=\"http://tempuri.org/\">\n";
request += "<firstNum>5</firstNum>\n";
request += "<secondNum>2</secondNum>\n";
request += "</Add>\n";
request += "</soap:Body>\n";
request += "</soap:Envelope>\n\n";
action = "http://tempuri.org/Add";
return request;
}
public static byte[] ConvertStringToBytes(string input)
{
MemoryStream stream = new MemoryStream();
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(input);
writer.Flush();
}
return stream.ToArray();
}
public static void Main()
{
byte[] body = ConvertStringToBytes(CreateSoapEnvelope());
// Create an HTTP Web request.
HttpWebRequest request =
HttpWebRequest.Create(url) as HttpWebRequest;
// set the request type
// we user utf-8 but set the content type here
request.Method = "POST";
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.ContentLength = body.Length;
// add the headers
// the SOAPACtion determines what action the web service should use
request.Headers.Add("SOAPAction", action);
// Set request.KeepAlive to use a persistent connection.
request.KeepAlive = false;
// add our body to the request
Stopwatch sw1 = Stopwatch.StartNew();
Stopwatch sw2 = Stopwatch.StartNew();
Stream stream = request.GetRequestStream();
Stopwatch sw4 = Stopwatch.StartNew();
stream.Write(body, 0, body.Length);
sw4.Stop();
stream.Close();
sw2.Stop();
// Get a response from the server.
Debug.Print("Request : " + DateTime.Now.ToString() + "-" + DateTime.Now.Millisecond);
WebResponse resp = null;
Stopwatch sw3 = null;
try
{
sw3 = Stopwatch.StartNew();
resp = request.GetResponse();
sw3.Stop();
}
catch (Exception e)
{
Debug.Print("Exception in HttpWebRequest.GetResponse(): " +
e.ToString());
}
sw1.Stop();
Debug.Print("stream taked : " + sw2.ElapsedMilliseconds + " ms");
Debug.Print("stream.Write taked : " + sw4.ElapsedMilliseconds + " ms");
Debug.Print("request.GetResponse taked : " + sw3.ElapsedMilliseconds + " ms");
Debug.Print("total taked : " + sw1.ElapsedMilliseconds + " ms");
// Get the network response stream to read the page data.
if (resp != null)
{
sw1 = Stopwatch.StartNew();
Stream respStream = resp.GetResponseStream();
string page = null;
byte[] byteData = new byte[4096];
char[] charData = new char[4096];
int bytesRead = 0;
Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
int totalBytes = 0;
// allow 2 seconds for reading the stream
respStream.ReadTimeout = 2000;
// If we know the content length, read exactly that amount of
// data; otherwise, read until there is nothing left to read.
if (resp.ContentLength != -1)
{
Debug.Print("Response : " + DateTime.Now.ToString() + "-" + DateTime.Now.Millisecond);
for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
{
Thread.Sleep(500);
Stopwatch sw5 = Stopwatch.StartNew();
bytesRead =
respStream.Read(byteData, 0, byteData.Length);
sw5.Stop();
if (bytesRead == 0)
{
Debug.Print("Error: Received " +
(resp.ContentLength - dataRem) + " Out of " +
resp.ContentLength);
break;
}
dataRem -= bytesRead;
// Convert from bytes to chars, and add to the page
// string.
int byteUsed, charUsed;
bool completed = false;
totalBytes += bytesRead;
UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0,
bytesRead, true, out byteUsed, out charUsed,
out completed);
page = page + new String(charData, 0, charUsed);
// Display the page download status.
Debug.Print("Bytes Read Now: " + bytesRead +
" Total: " + totalBytes);
Debug.Print("respStream.Read : " + sw5.ElapsedMilliseconds + " ms");
}
page = new String(
System.Text.Encoding.UTF8.GetChars(byteData));
}
else
{
// Read until the end of the data is reached.
while (true)
{
// If the Read method times out, it throws an exception,
// which is expected for Keep-Alive streams because the
// connection isn't terminated.
try
{
Thread.Sleep(500);
bytesRead =
respStream.Read(byteData, 0, byteData.Length);
}
catch (Exception)
{
bytesRead = 0;
}
// Zero bytes indicates the connection has been closed
// by the server.
if (bytesRead == 0)
{
break;
}
int byteUsed, charUsed;
bool completed = false;
totalBytes += bytesRead;
UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0,
bytesRead, true, out byteUsed, out charUsed,
out completed);
page = page + new String(charData, 0, charUsed);
// Display page download status.
Debug.Print("Bytes Read Now: " + bytesRead +
" Total: " + totalBytes);
}
Debug.Print("Total bytes downloaded in message body : "
+ totalBytes);
}
// Display the page results.
Debug.Print(page);
// Close the response stream. For Keep-Alive streams, the
// stream will remain open and will be pushed into the unused
// stream list.
resp.Close();
sw1.Stop();
Debug.Print("response treatment taked : " + sw1.ElapsedMilliseconds + " ms");
}
}
}
}
I’ve got :
sw1 on writting = 500 ms … 4129 ms
sw2 = 292 ms … 3963 ms
sw3 = 3 ms
sw4 = 144 ms
sw1 or reception = 542 ms