Get Asp Ajax/JSON in WebRequest example
i like to do my crawling with WebRequest, so i found an ajax i need, and if u never noticed, Asp Ajax Controls use JSON, so.. how u do that?
1st of all take a look at my normal WebRequest method:
http://bresleveloper.blogspot.com/2012/04/ease-of-webrequest.html
atcualy there r only 2 things to change, the ContentType and the RequestStream.
ContentType is easy => "application/json; charset=utf-8"
now the stream must be built differently, as a JSON, and dont use the JavaScriptSerializer Serialize mehtod cuz u'll get key-pair and thats not what u want, instead lets change all the part with the
bool first = true;
first = false;
sbData.Append("\"" + key + "\"");
}
tada!!!!
bool first = true;
StringBuilder sbData = new StringBuilder();
foreach (KeyValuePair<string, List<string>> entry in parameters)
{
if (first)
{
first = false;
sbData.Append("{");
}
else
sbData.Append(",");
sbData.Append("\"" + entry.Key + "\"");
if (entry.Value.Count > 1)
{
sbData.Append(":[");
bool firstValue = true;
foreach (string val in entry.Value)
{
if (firstValue)
firstValue = false;
else
sbData.Append(",");
sbData.Append("\"" + val + "\"");
}
sbData.Append("]");
}
else
sbData.Append(":\"" + entry.Value[0] + "\"");
}
sbData.Append("}");
atcualy there r only 2 things to change, the ContentType and the RequestStream.
ContentType is easy => "application/json; charset=utf-8"
now the stream must be built differently, as a JSON, and dont use the JavaScriptSerializer Serialize mehtod cuz u'll get key-pair and thats not what u want, instead lets change all the part with the
StringBuilder sbData until we get this:
*Note - do remember that the members comes like this:
Dictionary<string, object> and object again becomes this dic until he become an ArrayList of string.
*Note - do remember that the members comes like this:
Dictionary<string, object> and object again becomes this dic until he become an ArrayList of string.
bool first = true;
StringBuilder sbData = new StringBuilder();
foreach (String key in parameters.AllKeys)
{
if (first)
{
first = false;
sbData.Append("{");
}
else
sbData.Append(",");
else
sbData.Append(":");
sbData.Append("\"" + parameters[key] + "\"");
}
sbData.Append("}");
sbData.Append("}");
if ("POST" == request.Method)
{
request.ContentType = "application/json; charset=utf-8";
request.ContentLength = sbData.Length;
request.ContentType = "application/json; charset=utf-8";
}
tada!!!!
p.s. - if u want to send lists in parameters u can change parameters to
Dictionary<string, List<string>> and do this:
bool first = true;
StringBuilder sbData = new StringBuilder();
foreach (KeyValuePair<string, List<string>> entry in parameters)
{
if (first)
{
first = false;
sbData.Append("{");
}
else
sbData.Append(",");
sbData.Append("\"" + entry.Key + "\"");
if (entry.Value.Count > 1)
{
sbData.Append(":[");
bool firstValue = true;
foreach (string val in entry.Value)
{
if (firstValue)
firstValue = false;
else
sbData.Append(",");
sbData.Append("\"" + val + "\"");
}
sbData.Append("]");
}
else
sbData.Append(":\"" + entry.Value[0] + "\"");
}
sbData.Append("}");
Comments
Post a Comment