Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using same connection for post/get

Status
Not open for further replies.

dubBeat

Programmer
Apr 2, 2007
21
IE
Heya,

Im having some trouble with Post/Get. Ive got a web app that accepts a post. This post generates a link to a get request. The application works fine e.g

(.... please bear with me here. its a little difficult for me to describe.)

> open the app in a browser
> enter a number, press submit and go to page with link
> click link to see result

The link would be something like /info?id=12&typr=slir and displays data. I can open up as many new tabs in firefox as I like, copy the link address in, and it shows me the data.

(*1)If I where to open a new browser window and type the generated link into the address bar it contains no data.

*****Thats the background here is my problem*******

Im trying to imitate all this user interaction with c#. The post works fine.

The trouble is when I come to the get part. It returns no data. I think the reason for this is similar to the action in *1. That is, opening the get request link in a different browser.

I think that when I make my get request a new connection is made and the instance no longer exists?

first off, do you think the problem lays where I suggested (the example of opening another window)?

And if so could you suggest a solution?
do i create a proxy?

do i send the post and get with the same stream?

I'd really appreciate any ideas,hypotesis, suggestions. Anything at all

***************Post***************************
string url = " string result = string.Empty;
string postData = "msisdn0=123";
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x- request.ContentLength = postData.Length;
Stream writeStream = request.GetRequestStream();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
result = readStream.ReadToEnd();
*********************************************************

***************Get***************************
string url2 = " string result2 = string.Empty;
string postData2 = "info?id=862324984&type=slir";
Uri uri2 = new Uri(url2);
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(uri2);
request2.Method = "GET";
request2.ContentType = "application/x- request2.ContentLength = postData2.Length;
Stream writeStream2 = request2.GetRequestStream();
UTF8Encoding encoding2 = new UTF8Encoding();
byte[] bytes2 = encoding.GetBytes(postData2);
writeStream2.Write(bytes, 0, bytes.Length);
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
Stream responseStream2 = response2.GetResponseStream();
StreamReader readStream2 = new StreamReader(responseStream2, Encoding.UTF8);
result2 = readStream.ReadToEnd();
testText.Text = result2;
*********************************************************
 
Is the server side data generation dependant upon some kind of session or a cookie?
 
Im not sure to be honest but I dont think that it is. The data is generated from a servlet.
 
Ive been trying a few different things over the past few days such as the keepalive method and making the cokkie of response 2 equal the cookie for request 1 but nothing seems to work.

I Cant figure out what Im missing?
 
I was thinking about this a bit more. Im new to c# and this level of http so im not too sure what tools are at my disposal.

Would I create a cookie the first time the "post method" connects and then use that cookie for my second request "my get method".?

How might I get the information I need from the browser to a cookie? Are they automatically generated?

Again any ideas would be appreciated

 
Cookies are normally created by the server application and then sent to your browser. The browser then sends the cookie back to the server for all future requests to the same web address. Cookies can either expire after a certain amount of time, or they can be set to expire when the browser closes.

Cookies are often used to store a session ID so that when you visit a site for a second time it the server can match your request up with some data it generated on the first request.

You say the web app follows these steps:

1. open the app in a browser
2. enter a number, press submit and go to page with link
3. click link to see result

My guess would be that the server is creating the data on step 2 and is then setting a cookie so that when you perform step 3 it can find the data it generated for you on step 2.

You are using HttpWebRequest for your first call. Unlike a browser this has not built in support for cookies. You will need to check the HTTP headers in the response to see if the server is returning a cookie to you. If it is, you will need to send this back in the HTTP header when you make your second GET request.

This whole cookie thing could just be a red herring though.
 
Thanks very much for that explanation of the process.

I really am a complete cookie virgin.

I checked the header of the first response and it does generate a cookie.

*******************************
Pragma: no-cache
Content-Length: 400
Content-Type: text/html
Date: Fri, 13 Apr 2007 15:09:10 GMT
Set-Cookie: JSESSIONID=7324232D88C3C3C14097FD0B5326B4AC;Path=/
Server: Apache Tomcat/4.1.18-LE-jdk14 (HTTP/1.1 Connector)
***********************************

So where it says JSessionID i have to make the second request have that same Jsession ID?
 
Ive being trying this approach and am getting an error saying "The server committed a protocol violation. Section=ResponseStatusLine"

What does this mean?

> I first tried capturing the JSESSION ID from the first "posts" response like so.

>>> for (int i = 0; i < response.Headers.Count; i++)
{
if (response.Headers.GetKey(i) == "Date") {
string date = response.Headers.Get(i);
testText.Text = date;
}
if (response.Headers.GetKey(i) == "Set-Cookie")
{
string id = response.Headers.Get(i);

}

HttpCookie myCookie = new HttpCookie("Connection");
myCookie["JSESSIONID"] = "id";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);


cookieList.Items.Add("\t" + response.Headers.GetKey(i) + " = " + response.Headers.Get(i));
}

///////////////////////////////////////

So I made a cookie with the JSessionID as a property.

> When I get to the second "get" request

I try to make its cookie equal to the one ive made

>>>HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(uri2);
request2.Method = "GET";

for (int i = 0; i < response.Headers.Count; i++){
if (Response.Cookies.ToString()=="ConnectionSettings") {

response.Cookies.Equals(Response.Cookies);
// response2 = (HttpWebResponse)request2.GetResponse();

}

}

HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();

//////////////////////////////////////////////////////

Am I approaching this the correct way to you think?

What would be a better solution to the way i am trying?



(I know it might not even be a cookie problem but id like to exhaust all avenues to be sure)

 
Hi ,

I looked at more of the header from the first request.


Its got things like

**************

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUINTY1MjkxODgPZBYCAgMPZBYCAgEPEGQPFgVmAgECAgIDAgQWBRAFEglQcmFnbWEgPSBuby1jYWNoZQUSCVByYWdtYSA9IG5vLWNhY2hlZxAFFQlDb250ZW50LUxlbmd0aCA9IDQwMAUVCUNvbnRlbnQtTGVuZ3RoID0gNDAwZxAFGQlDb250ZW50LVR5cGUgPSB0ZXh0L2h0bWwFGQlDb250ZW50LVR5cGUgPSB0ZXh0L2h0bWxnEAUlCURhdGUgPSBGcmksIDEzIEFwciAyMDA3IDE4OjUzOjM2IEdNVAUlCURhdGUgPSBGcmksIDEzIEFwciAyMDA3IDE4OjUzOjM2IEdNVGcQBUAJU2V0LUNvb2tpZSA9IEpTRVNTSU9OSUQ9NkQyREIyOUE3MTY0MjlCQUI0RDlCMUI4REJENjcxRUM7UGF0aD0vBUAJU2V0LUNvb2tpZSA9IEpTRVNTSU9OSUQ9NkQyREIyOUE3MTY0MjlCQUI0RDlCMUI4REJENjcxRUM7UGF0aD0vZ2RkZB5qLawe65riRxbSgMwND3x2E7iZ" />

***************

and

**************

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBwKV65WiAwKx2ZK9BQLvwtCVAQLK8uSKCALB2Z6FCQLguKzuDQK3tOfJBcTZBgTQwNKys62e6d3GTp4r8WMS" />

*************

do you think that the values of these "hidden" values would also need to be stored and used for further interaction.
 
Hi,

Try posting this on the ASP.NET forum. You may get more responses there.

[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top