How can I catch UTM Cookies values? - c#

I'm trying to Log into a website using WebRequests in C# But the website is using Google-Analytics libraries which is making some UTM-Cookies
The question is how can I catch those cookies values and pass them to the website?

The HttpWebRequest class has a CookieContainer collection property that you can add instances of the Cookie class.
The HttpWebResponse class has a Cookies collection property that contains instances of the Cookie class.
Make your request, read the cookies from the response, and add them to any subsequent requests as needed.

Related

How to access cookies sent from an url in windows phone application?

I'm calling a url in my app,and that url send me a data via cookies, how should i get that data from cookies?? Like NSHttpCookie and NSHttpSharedCookie in IOS.
1. If you're using the Windows Phone WebBrowser control, you can try to use the WebBrowserExtensions.GetCookies method :
You could use the GetCookies method to retrieve cookies associated
with a website if you use the WebBrowser control in your application.
Once you have retrieved a CookieCollection, you could use the cookies
to make subsequent HTTP requests to the website.
It should return a CookieCollection which contains Cookie instances from which you'll get all the information you need.
2. If you're using HttpWebRequest, you'll find a good tutorial from msdn in here.
Basically you have to create and to populate a CookieContainer instance from your HttpWebRequest to send cookies and you just have to get received cookies from the Cookies property of HttpWebResponse in the other way. (It also returns a CookieCollection)

How are Static Variables shared in a Web Session

I have an mvc webapi service setup that pulls and pushes data from an sql server database.
Within the Web project holding the webapi I have a Static class that just holds some global variables that are accessed from the webapi methods. Following is a very cutdown example of the static class:
public static class SystemProperties
{
public static int currentContactID;
}
When the WebApi is accessed I strip the ContactID from the Http Headers of the call and set the SystemProperties.CurrentContactID to it.
Than in the methods of the webapi I access SystemProperties.CurrentContactID for data calls.
I have found a problem when there is concurrent webapi calls the currentContactID is getting mixed up.
My question is, how is the static class members shared between calling sessions? Is it going to be pretty much last in best dressed and if the previous person is still in there than they will get screwed over by the new person who will overwrite the variables with their details?
Should I be using a different method to store these details?
Thanks in advance
You can use the Current HttpContext to store items to use across the lifetime of an HttpRequest
HttpContext.Current.Items["currentContactID"] = value
So you can grab the data from the Request header, and store it here and it will be available for the duration of the current http web request for that user. Every http request has its own Items dictionary so it won't be overwritten by simultaneous requests
If you need to store a variable across several requests for the same user you need to look into cookies or sessions.
Yes, you should be using different method.
With Web API the request comes in as HttpRequestMessage. You can implement a HTTP message handler (inheriting from DelegatingHandler) and from there strip the CurrentContactID from HTTP Headers. You can also take your CurrentContactID value you've stripped out and add it to the HttpRequestMessage objects Properties dictionary.
Here's article on implementing HTTP message handler: http://www.asp.net/web-api/overview/working-with-http/http-message-handlers
Here's article on the Properties property of HttpRequestMessage: http://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessage.properties.aspx
Keep in mind that the Properties property on HttpRequestMessage is available to you at controller as well which is why it is suitable for use in lieu of static class or session.

Is HttpContext.Current.Items thread-safe between Requests?

Suppose one has an HttpHandler that processes each request, and suppose each HttpHandler computes an intermediate result for each request and potentially wants to pass this to a page handler eg via Server.Transfer or Server.Execute via the HttpContext.Items collection
Will each request have a separate copy of HttpContext.Items["sameKey"] when they each reach the same .aspx page?
My concern arises from the fact HttpContext.Current is itself a static property
HttpContext Encapsulates all HTTP-specific information about an individual HTTP
request.
Hence each request HttpContext.Items["sameKey"] will be a different copy.
HttpContext.Items is stateless the only way to "share" between requests is Session or higher level state (database)

Change the Uri of a instance of HttpWebRequest?

I have an instance of an HttpWebRequest that I'm intercepting in an event.
I would like to edit the url before the request is sent but I can't find a way of doing this.
The property RequestUri is read only.
I've thought of a few ways but can't seem to find a working solution:
-Using reflection to set the value ?
-Creating a new request and then cloning all properties. not sure how to do that.
If you think in terms of the HTTP protocol, each request is stateless / unique. The only way to link one request to another is programmatically through something like Cookies, but to the HTTP protocol itself the request is unique.
I think the HttpWebRequest object was designed with this in mind. Each HttpWebRequest represents one unique call to a URL and you build up the parameters for that call. If you want to do another request to a different URL you would create a new HttpWebRequest and pass it the state information you are using, ie: Cookie container, header information etc.
The long winded answer to this is the object is designed to have a read only url and the only way to handle it is to:
Use a little reflection hack, such as you have already done, if you absolutely need to use the given HttpWebRequest object you have.
Create a new HttpWebRequest (WebRequest.Create()) and copy your state information over to the new request.
You can use RewritePath to do this.
F.e.
HttpContext.Current.RewritePath("newurl.aspx");

Download a file over HTTPS C# - Cookie and Header Prob?

I am trying to download a file over HTTPS and I just keep running into a brick wall with correctly setting Cookies and Headers.
Does anyone have/know of any code that I can review for doing this correctly ? i.e. download a file over https and set cookies/headers ?
Thanks!
I did this the other day, in summary you need to create a HttpWebRequest and HttpWepResponse to submit/receive data. Since you need to maintain cookies across multiple requests, you need to create a cookie container to hold your cookies. You can set header properties on request/response if needed as well....
Basic Concept:
Using System.Net;
// Create Cookie Container (Place to store cookies during multiple requests)
CookieContainer cookies = new CookieContainer();
// Request Page
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.amazon.com");
req.CookieContainer = cookies;
// Response Output (Could be page, PDF, csv, etc...)
HttpWebResponse resp= (HttpWebResponse)req.GetResponse();
// Add Response Cookies to Cookie Container
// I only had to do this for the first "login" request
cookies.Add(resp.Cookies);
The key to figuring this out is capturing the traffic for real request. I did this using Fiddler and over the course of a few captures (almost 10), I figured out what I need to do to reproduce the login to a site where I needed to run some reports based on different selection critera (date range, parts, etc..) and download the results into CSV files. It's working perfect, but Fiddler was the key to figuring it out.
http://www.fiddler2.com/fiddler2/
Good Luck.
Zach
This fellow wrote an application to download files using HTTP:
http://www.codeproject.com/KB/IP/DownloadDemo.aspx
Not quite sure what you mean by setting cookies and headers. Is that required by the site you are downloading from? If it is, what cookies and headers need to be set?
I've had good luck with the WebClient class. It's a wrapper for HttpWebRequest that can save a few lines of code: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

Categories

Resources