Calling a web service with asp.net that needs cookies

asp.net c# html-agility-pack web-services

Question

I'm attempting to scrape the list of attorneys at http://www.dlapiper.com/global/people/search.aspx?gLastName=A using the HTML Agility Pack. I haven't had much success trying to accomplish it server side since the site utilizes Ajax to generate the names. I tried to use the same web service from my application after seeing the site is calling one at http://www.dlapiper.com/FCWSite/DlaPiperWS/Attorneys.asmx?op=FindAttorneys.

I'm trying to use the following code, but it keeps giving me an Internal Server Error 500, which I think is because I need a session cookie. (since if I deactivate cookies, the site won't function)

Can anyone assist?

private static string WebServiceCall()
{

    WebRequest webRequest = WebRequest.Create("http://www.dlapiper.com/FCWSite/DlaPiperWS/Attorneys.asmx?op=FindAttorneys");
    HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
    httpRequest.Method = "POST";
    httpRequest.ContentType = "text/xml; charset=utf-8";
    httpRequest.Headers.Add("SOAPAction: http://tempuri.org/");
    httpRequest.ProtocolVersion = HttpVersion.Version11;
    Stream requestStream = httpRequest.GetRequestStream();
    //Create Stream and Complete Request             
    StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
    string 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/'><soap:Body><FindAttorneys xmlns='FCWSite.FCWSite.DlaPiperWS'><scFirstName></scFirstName><scLastName>A</scLastName><scKeyword></scKeyword><scOfficesGUID></scOfficesGUID><scSpokenLanguagesGUID></scSpokenLanguagesGUID><scServicesGUID></scServicesGUID><scRegionGUID></scRegionGUID><scSchoolGUID></scSchoolGUID><scAdmissionGUID></scAdmissionGUID><scLevelGUID></scLevelGUID><strLanguageGUID>7483b893-e478-44a4-8fed-f49aa917d8cf</strLanguageGUID><strCountry>global</strCountry><returnUntranslated>true</returnUntranslated><sortBy>name</sortBy><page>0</page></FindAttorneys></soap:Body></soap:Envelope>";

    streamWriter.Write(request);
    streamWriter.Close();
    //Get the Response    
    HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader srd = new StreamReader(wr.GetResponseStream());
    string resulXmlFromWebService = srd.ReadToEnd();
    return resulXmlFromWebService;
}
1
0
3/3/2014 6:21:31 AM

Accepted Answer

I included the Attorney.asmx address as a web reference in my project, as I said in my remark.

When doing so, I permitted the web reference to use the namespace "com.dlapiper.www" as the reference.

The following is written in VB.NET and is an example of a default page. The context of the connection is represented by the class com.dlapiper.www.Attorneys.

Take note that lawyers. A string value is returned by FindAttorney.

Additionally, my buffer and array let you view. If you attempt to execute FindAttorney in the code without adding the cookie container and the subsequent cookies, you will mistakenly get a SOAP error that claims the localizationGUID was not in the right input. This is because I was experimenting with various encodings.

Because I like contravariant programming, I left this pretty pointless section of code in the program.

The cookie name:value is what the function really uses, even though that argument can't be empty.

VB.NET

Public Class _Default
    Inherits System.Web.UI.Page

    Dim AttorneyList As com.dlapiper.www.Attorneys = New com.dlapiper.www.Attorneys()
    Dim attys As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim buffer As Byte()
        buffer = Array.ConvertAll(Of Char, Byte)("7483b893e47844a48fedf49aa917d8cf".ToUpper.ToCharArray(), New Converter(Of Char, Byte)(AddressOf CharToBuffer))

        Dim cookieLanguage As System.Net.Cookie = New System.Net.Cookie
        cookieLanguage.Domain = "www.dlapiper.com"
        cookieLanguage.Name = "Language"
        cookieLanguage.Value = UTF8Encoding.ASCII.GetString(buffer)
        cookieLanguage.Path = "/"

        Dim cookieLocalization As System.Net.Cookie = New System.Net.Cookie
        cookieLocalization.Domain = "www.dlapiper.com"
        cookieLocalization.Name = "Localization"
        cookieLocalization.Value = "TimeZone=0&UsesDaylightSavings=False&TimeZoneAbbrev=IDLW&Persists=True"
        cookieLocalization.Path = "/"

        Dim cookieCulture As System.Net.Cookie = New System.Net.Cookie
        cookieCulture.Domain = "www.dlapiper.com"
        cookieCulture.Name = "DefaultCulture"
        cookieCulture.Value = "en-US"
        cookieCulture.Path = "/"

        Dim cookieHideNotice As System.Net.Cookie = New System.Net.Cookie
        cookieHideNotice.Domain = "www.dlapiper.com"
        cookieHideNotice.Name = "hide-cookie-notice"
        cookieHideNotice.Value = "1"
        cookieHideNotice.Path = "/"

        Dim cookieMode As System.Net.Cookie = New System.Net.Cookie
        cookieMode.Domain = "www.dlapiper.com"
        cookieMode.Name = "Mode"
        cookieMode.Value = "1"
        cookieMode.Path = "/"

        Dim cookieNavId As System.Net.Cookie = New System.Net.Cookie
        cookieNavId.Domain = "www.dlapiper.com"
        cookieNavId.Name = "NavId"
        cookieNavId.Value = "1074"
        cookieNavId.Path = "/"

        Dim cookiePortletId As System.Net.Cookie = New System.Net.Cookie
        cookiePortletId.Domain = "www.dlapiper.com"
        cookiePortletId.Name = "PortletId"
        cookiePortletId.Value = "12601"
        cookiePortletId.Path = "/"

        Dim cookieSERVER_PORT As System.Net.Cookie = New System.Net.Cookie
        cookieSERVER_PORT.Domain = "www.dlapiper.com"
        cookieSERVER_PORT.Name = "SERVER_PORT"
        cookieSERVER_PORT.Value = "80"
        cookieSERVER_PORT.Path = "/"

        Dim cookieSiteId As System.Net.Cookie = New System.Net.Cookie
        cookieSiteId.Domain = "www.dlapiper.com"
        cookieSiteId.Name = "SiteId"
        cookieSiteId.Value = "1039"
        cookieSiteId.Path = "/"

        Dim cookieZoneId As System.Net.Cookie = New System.Net.Cookie
        cookieZoneId.Domain = "www.dlapiper.com"
        cookieZoneId.Name = "ZoneId"
        cookieZoneId.Value = "8"
        cookieZoneId.Path = "/"

        Dim cookieEventingStatus As System.Net.Cookie = New System.Net.Cookie
        cookieEventingStatus.Domain = "www.dlapiper.com"
        cookieEventingStatus.Name = "EventingStatus"
        cookieEventingStatus.Value = "1"
        cookieEventingStatus.Path = "/"


        AttorneyList.CookieContainer = New System.Net.CookieContainer()

        AttorneyList.CookieContainer.Add(cookieLanguage)
        AttorneyList.CookieContainer.Add(cookieLocalization)
        AttorneyList.CookieContainer.Add(cookieCulture)
        AttorneyList.CookieContainer.Add(cookieMode)
        AttorneyList.CookieContainer.Add(cookieEventingStatus)
        AttorneyList.CookieContainer.Add(cookieNavId)
        AttorneyList.CookieContainer.Add(cookieSiteId)
        AttorneyList.CookieContainer.Add(cookieSERVER_PORT)
        AttorneyList.CookieContainer.Add(cookieHideNotice)
        AttorneyList.CookieContainer.Add(cookiePortletId)
        AttorneyList.CookieContainer.Add(cookieZoneId)

        'UTF8Encoding.ASCII.GetString(buffer)
        attys = AttorneyList.FindAttorneys("B", "", "", "", "", "", "", "", "", "", UTF8Encoding.ASCII.GetString(buffer), "global", False, "name", 0)
        Response.Write(attys)
    End Sub

    Function CharToBuffer(ByVal character As Char) As Byte
        Return Convert.ToByte(character)
    End Function
End Class
1
3/3/2014 8:56:49 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow