-1

Hi I have this following code for C#.Net

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "membershipcardnumber=" + Cardnumber;
postData += ("&terminalname=" + TerminalName);
postData += ("&serviceid=" + CasinoID);
byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest =
  (HttpWebRequest)WebRequest.Create("http://myurl");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();

newStream.Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format. 
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
var egm_response = readStream.ReadToEnd();
Console.WriteLine(egm_response);

the variable egm_response outputs the entire html code. This is just a part of it:

<div class="result" style="margin-left: 20px;">
        <p>JSON Result :</p>
    {"CreateEgmSession":{"IsStarted":0,"DateCreated":"","TransactionMessage":"Terminal already has an active session.","ErrorCode":37}}    </div>
<div class="clear"></div>

How can I get or parse this html and only get the value after ErrorCode?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
jackhammer013
  • 2,295
  • 11
  • 45
  • 95

1 Answers1

1

First, the service should return JSON, not a combination of HTML and JSON. If it is JSON, you could parse the JSON response into a .NET object using JSON.NET, which you can ask for the ErrorCode property.

Second, you could use regex to match the ErrorCode:

"ErrorCode"\:\s+(\d+)

I would advise to go for option 1 if possible, since that would make your life a lot easier.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325