Catching the Location of a POST

Asked

Viewed 94 times

2

I’m using the method HttpRequestHeader to make requests (GET and POST), but I need to take the parameter Location of the response of a POST and I am unable to evolve. There is an example of my request below:

url = "https://www.google.com"
        req = HttpWebRequest.Create(url)
        req.Method = "POST"
        req.Headers.Add(HttpRequestHeader.Cookie, sessao)
        req.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR")
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
        req.Referer = "www.google.com"
        req.ContentType = " multipart/form-data; boundary=---------------------------24708330210100"
        req.ContentLength = postData.Length

        Dim swRequestWriter As StreamWriter = New StreamWriter(req.GetRequestStream())
        swRequestWriter.Write(postData)
        swRequestWriter.Close()

        Dim srResponseReader As StreamReader = New StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.UTF7)

        html = srResponseReader.ReadToEnd().Trim

Remembering that the addresses are fictitious, I would like an urgent help.

2 answers

2

I think we get the Header solves what you need. Replace the end of your code with something like this:

using var response = req.GetResponse()) ;
var location = response.Headers["Location"];
using var srResponseReader = new StreamReader(response.GetResponseStream()));
html = srResponseReader.ReadToEnd();

I put in the Github for future reference.

1

I think you have to pick up and check the Header

HttpWebResponse response = myRequest.GetResponse();

// verifica se existe o location
if( response.Headers["Location"] == null ) {    
}
else{
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.