C# 使用asp.net调用需要Cookie的web服务
我正试图使用HTML敏捷包来抓取律师的名单。该站点使用Ajax来填充名称,因此我在服务器端没有任何运气。我注意到该站点正在调用一个web服务,所以我尝试从我的应用程序调用同一个web服务 我正在使用以下代码,但它返回一个内部服务器错误500,我怀疑这是因为我需要会话cookie?(因为如果我禁用Cookie,站点将无法工作) 有人能帮忙吗C# 使用asp.net调用需要Cookie的web服务,c#,asp.net,web-services,html-agility-pack,C#,Asp.net,Web Services,Html Agility Pack,我正试图使用HTML敏捷包来抓取律师的名单。该站点使用Ajax来填充名称,因此我在服务器端没有任何运气。我注意到该站点正在调用一个web服务,所以我尝试从我的应用程序调用同一个web服务 我正在使用以下代码,但它返回一个内部服务器错误500,我怀疑这是因为我需要会话cookie?(因为如果我禁用Cookie,站点将无法工作) 有人能帮忙吗 private static string WebServiceCall() { WebRequest webRequest = WebReques
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;
}
私有静态字符串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();
//创建流并完成请求
StreamWriter StreamWriter=新的StreamWriter(requestStream,Encoding.ASCII);
字符串请求=“A7483b893-e478-44a4-8fed-f49aa917d8cfglobaltruename0”;
streamWriter.Write(请求);
streamWriter.Close();
//得到回应
HttpWebResponse wr=(HttpWebResponse)httpRequest.GetResponse();
StreamReader srd=新的StreamReader(wr.GetResponseStream());
字符串resultXmlFromWebService=srd.ReadToEnd();
返回结果xmlfromWebService;
}
如我在评论中所述。。我在我的项目中添加了authority.asmx url作为web参考
这样做时,我让web引用的命名空间为'com.dlapiper.www'
以下内容在vb.net中,表示标准的默认页面。类com.dlapiper.www.Attorneys表示连接的上下文
请注意,Attorneys.FindAttorney返回一个字符串值
另外,您可以从代码中的buffer和array.convertAll尝试中看到,我尝试了不同的编码。。如果尝试调用FindAtorney而不添加cookie容器和以下cookie。。您将错误地得到一个SOAP异常,指出本地化GUID不在正确的输入中
我在代码中留下了一些不相关的部分,因为我是逆变编程的爱好者
事实上,该参数不能为空,但函数实际上没有使用它。。cookie名称:值为
VB.NET
您是否尝试过将其作为web引用添加到您的项目中。这相当简单。这真的很有帮助,而且效果很好,谢谢!
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