Need help with XMLHttprequest

mtantouri

Client
Регистрация
11.08.2013
Сообщения
32
Благодарностей
1
Баллы
8
I really appreciate response from fellow members and for the right one, even pay for your time.

I got this code, from a developer who does not know anything about zennoposter:

Код:
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace MerInfo
{
    class CallMerInfo
    {
        private string _uniqid;
        private string _csrfcookiename;
        private CookieContainer _cookieJar;
        private string _returnedresponse;

        private string GetRawResponse(string who, string location)
{
    _returnedresponse = "";
   
    var uri = "http://www.merinfo.se/search?who=" + who + "&location=" + location;
    Getcsrfcookiename(uri);
    var request = WebRequest.Create(uri);
    request.Method = "POST";
    var postData = "";
    var byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteArray.Length;
    var dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    var response = request.GetResponse();
    dataStream = response.GetResponseStream();

    if (dataStream != null)
    {
        var reader = new StreamReader(dataStream);
        var responseFromServer = reader.ReadToEnd();
        var positionstart = responseFromServer.IndexOf("'uniqid': '", System.StringComparison.Ordinal);
        var positionend = responseFromServer.IndexOf("',", positionstart + 11, System.StringComparison.Ordinal);

        _uniqid = responseFromServer.Substring(positionstart + 11, positionend - (positionstart + 11));

        reader.Close();
    }

    if (dataStream != null) dataStream.Close();
    response.Close();

    uri = "http://www.merinfo.se/ajax/search";
    postData = "uniqid=" + _uniqid + "&csrf_test_name=" + _csrfcookiename;
    GetSecondResponse(uri, postData);
   
    return _returnedresponse;
}

        private void Getcsrfcookiename(string uri)
        {
            _cookieJar = new CookieContainer();

            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.CookieContainer = _cookieJar;

            var response = request.GetResponse();

            foreach (var c in from Cookie c in _cookieJar.GetCookies(request.RequestUri) where c.Name.ToString(CultureInfo.InvariantCulture) == "csrf_cookie_name" select c)
            {
                _csrfcookiename = c.Value.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void GetSecondResponse(string uri, string postData)
        {
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.CookieContainer = _cookieJar;
            request.Method = "POST";
            var byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            var dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            var response = request.GetResponse();
            dataStream = response.GetResponseStream();

            if (dataStream != null)
            {
                var reader = new StreamReader(dataStream);
                var responseFromServer = reader.ReadToEnd();

                _returnedresponse = "";
                _returnedresponse = @"HTTP/" + ((HttpWebResponse)response).ProtocolVersion.Major.ToString() + @"." + ((HttpWebResponse)response).ProtocolVersion.Minor.ToString() + @" " + ((HttpWebResponse)response).StatusDescription + Environment.NewLine;
                _returnedresponse += response.Headers.ToString();
                _returnedresponse += responseFromServer;

                //_returnedresponse = responseFromServer.Replace(",", "," + Environment.NewLine);

                reader.Close();
            }

            if (dataStream != null) dataStream.Close();
            response.Close();
        }
    }
}
Even I run this code in project maker I will receive the following error:

"} expected". [Row: 6; Column: 19]
"Invalid token 'namespace' in class, struct, or interface member declaration". [Row: 8; Column: 1]
"Invalid token '{' in class, struct, or interface member declaration". [Row: 9; Column: 1]
"A namespace cannot directly contain members such as fields or methods". [Row: 107; Column: 1]
"Type or namespace definition, or end-of-file expected". [Row: 109; Column: 1]
"Type or namespace definition, or end-of-file expected". [Row: 110; Column: 1]

Can anyone help me fix this? Please.

-MT
 
  • Спасибо
Реакции: spyder

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Well you could do something different. I don't know what to search for here on this site, but you can do it with regular Get and Post requests included with zenno. Here's an example. You will just have to replace the url string in the second GET with what your search variables are.
 

Вложения

mtantouri

Client
Регистрация
11.08.2013
Сообщения
32
Благодарностей
1
Баллы
8
I really appreciated your response bigcajones,

I already tried that and is working as your example you've giving. The problem is, why I need XMLHttprequest is because, this what happens in the last post in your example:

Код:
$.ajax({
                type: "POST",
                url: '/ajax/search',
                data: {
                    'uniqid': '53785dd0e4796',
                    'csrf_test_name': $.cookie('csrf_cookie_name')
                },
                dataType: 'json',
                success: function(data) {
                    if(data.objects.length > 0) {
                        $.each(data.objects, function(index, val) {
                            if(index == 1) {
                                                                                            }

                            if(index == 4) {
                                                                                            }

                            $('#searchresults #searchlist').append(template(val));
                        });
                    } else {
                        $('#searchresults').html('Din sökning matchade ingen person.');
                    }
                   
                    $('#searchresults').hide().fadeIn();
                },
                complete: function(){
                    $('#spinner').hide();
                },
                error: function(){
                    $('#searchresults').html('Någonting gick fel. Var god försök igen.');
                    $('#searchresults').hide().fadeIn();
                },
            });
Let's say we do a search with
Код:
http://www.merinfo.se/search?who=19140918-6622&location=
The normal get and post requests in project maker won't return this:

Код:
{"objects":[{"firstNameMarked":"<strong>Gunna<\/strong> Margareta","middleName":"","lastName":"Caspersson","ssn":"19140918-XXXX","profile":"\/person\/Karlskoga\/Gunna-Margareta-Caspersson-1914\/be986-4me","isMan":false,"hasCompany":false,"hasProperty":false,"isMarried":false,"coadress":"","gatuadress":"Bohultsgatan 8 B lgh 1203","postort":"Karlskoga","postnummer":"691 33","telefonnummer":"0586-306 45","telefonnummerUnformatted":"4658630645","mapUrl":"\/person\/Karlskoga\/Gunna-Margareta-Caspersson-1914\/be986-4me\/karta","isCeleb":false,"othersUrl":"\/search?ae6=Bohultsgatan+8+B+lgh+1203&ae4=Karlskoga&d=p"}],"status":0}[CODE]

The above will be return as is when done with XMLHttprequest.
 

mtantouri

Client
Регистрация
11.08.2013
Сообщения
32
Благодарностей
1
Баллы
8
Well you could do something different. I don't know what to search for here on this site, but you can do it with regular Get and Post requests included with zenno. Here's an example. You will just have to replace the url string in the second GET with what your search variables are.
After looking at your template again. It finally worked!
You did it BigcaJones, :D boy am I glad!
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
Anytime
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)