-
May 24th, 2016, 02:58 PM
#1
Thread Starter
Fanatic Member
CookieContainer SetCookie error
I have a request function that allows you to pass url and parameters and will return source which typically works but just not getting an error in the HttpWebResponse cookie header.
The error I get is:
System.Net.CookieException: An error occurred when parsing the Cookie header for Uri 'http://www.indeed.com/resumes?q=fabaceous&l=55378&co=US&sort=date&radius=25'. ---> System.Net.CookieException: The 'Path'='/me' part of the cookie is invalid.
at System.Net.Cookie.VerifySetDefaults(CookieVariant variant, Uri uri, Boolean isLocalDomain, String localDomain, Boolean set_default, Boolean isThrow)
at System.Net.CookieContainer.CookieCutter(Uri uri, String headerName, String setCookieHeader, Boolean isThrow)
--- End of inner exception stack trace ---
Here is the part of the code it's failing:
Code:
Try
If Not m_hwrResponse.Headers("Set-Cookie") Is Nothing Then
Dim ccContainer As New CookieContainer()
ccContainer = New CookieContainer()
ccContainer.SetCookies(m_hwrResponse.ResponseUri, m_hwrResponse.Headers("Set-Cookie"))
m_ccCookies.Add(ccContainer.GetCookies(m_hwrResponse.ResponseUri))
LogIt(" + [END] Set Response Header cookies ")
End If
Catch ex As Exception
LogIt("See -set cookie header error details in 'Error' log")
LogItError("-set cookie header error: " & ex.ToString)
LogIt(" + Response uri: " & m_hwrResponse.ResponseUri.ToString)
LogIt(" + Response headers (""Set-Cookie""): " & m_hwrResponse.Headers("Set-Cookie").ToString)
End Try
The weird thing is that if I break this out to it's only process in a button event it works file as shown below:
HTML Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="zCookie.aspx.vb" Inherits="zCookie" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<asp:Literal ID="litOutput" runat="server" />
<div>
</div>
</form>
</body>
</html>
Code:
Imports System.Net
Partial Class zCookie
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sResponseUri As String = "http://www.indeed.com/resumes?q=fabaceous&l=55378&co=US&sort=date&radius=25"
Dim sResponseHeader As String = "CSRF=biNIffjGBgwg0DHR7jh5NX2L4PoRtFfa; Domain=.indeed.com; Path=/; HttpOnly,LC=""co=US""; Version=1; Domain=.indeed.com; Max-Age=31536000; Expires=Wed, 24-May-2017 19:17:29 GMT; Path=/; HttpOnly,TOTS=1; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/resumes,TOTS=1; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/r,TOTS=1; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/me,CIS=1; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/resumes,CIS=1; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/r,CIS=1; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/me,RXSP=""q=fabaceous&l=55378&id=-8569913450055074882""; Version=1; Max-Age=536870911; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/resumes,RXSP=""q=fabaceous&l=55378&id=-8569913450055074882""; Version=1; Max-Age=536870911; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/r,RXSP=""q=fabaceous&l=55378&id=-8569913450055074882""; Version=1; Max-Age=536870911; Expires=Sun, 29-May-2033 14:06:01 GMT; Path=/me,BIGipServerresume_ord=!rGDIPZXP/j5dqc8/0rl98CFw+W0yBhjO7Kp8tA9ZTHrFXFotJdXwHoBQfLBGmUPOQRZS7cd+KTZnFck=; path=/"
Dim uriSite As New Uri(sResponseUri)
litOutput.Text = ""
Try
Dim ccContainer As New CookieContainer()
ccContainer = New CookieContainer()
ccContainer.SetCookies(uriSite, sResponseUri)
litOutput.Text = "added cookie"
Catch ex As Exception
litOutput.Text = "Error adding cookie: " & ex.ToString
End Try
End Sub
End Class
Any thoughts on why it may error or a fix? Thanks in advance.
-
May 25th, 2016, 04:12 AM
#2
Re: CookieContainer SetCookie error
How about encoding the cookie value first before assigning it to the CookieContainer object?
Csharp Code:
ccContainer.SetCookies(m_hwrResponse.ResponseUri, HttpUtility.UrlEncode(m_hwrResponse.Headers["Set-Cookie"], Encoding.GetEncoding("iso-8859-1")));
The snippet provided is in C#. But the idea is there. You may also replace iso-8859-1 with the appropriate encoding.
Last edited by KGComputers; May 25th, 2016 at 04:16 AM.
-
May 27th, 2016, 07:30 AM
#3
Thread Starter
Fanatic Member
Re: CookieContainer SetCookie error
Thanks KGComputers, tried and posting site didn't like me doing that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|