Foros del Web » Programación para mayores de 30 ;) » .NET »

Codigo Post

Estas en el tema de Codigo Post en el foro de .NET en Foros del Web. using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestPostExample { public static void Main() { // Create a request using ...
  #1 (permalink)  
Antiguo 24/08/2007, 06:57
 
Fecha de Ingreso: julio-2006
Mensajes: 150
Antigüedad: 18 años, 7 meses
Puntos: 0
Codigo Post

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
public class WebRequestPostExample
{
public static void Main()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://www.abc.com");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "http://www.farmatodo.com/buscadornew/productos.asp";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).Stat usDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
StreamWriter x = new StreamWriter("C://xx.html");
x.WriteLine(responseFromServer);
//Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
}
}

Ese es el codigo que consegui para obtener datos de una url por el protocolo post la cuestion es que eso me jala el codigo fuente de una pagina , pero lo que necesito saber es como se construye la url con el metodo post, me explico mejor :

Con el metodo GET la url es como la siguiente http://www.forosdelweb.com/newthread...newthread&f=29 donde la pagina base es la de foros del web y los agregados es para llegar al hilo de este mensaje como hago esa construccion cuando el metodo utilizado es el POST

gracias Ernesto
  #2 (permalink)  
Antiguo 24/08/2007, 07:14
Avatar de xknown  
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 20 años, 2 meses
Puntos: 7
Re: Codigo Post

No entendí muy bien lo que quieres hacer, pero si lo que preguntas es como enviar parámetros, postData tiene que tener la siguiente estructura:
variable1=valor1&variable2=valor2

Ten en cuenta que si valor1 o valor2 contienen caracteres especiales, antes tienes que codificarlos (UrlEncode).

Por otro lado, si usas la clase WebClient, vas a ahorrarte algunas líneas de código y posiblemente tengas menos errores...

Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño
  #3 (permalink)  
Antiguo 24/08/2007, 10:29
 
Fecha de Ingreso: julio-2006
Mensajes: 150
Antigüedad: 18 años, 7 meses
Puntos: 0
Re: Codigo Post

Si necesito ver como paso a una pagina que use POST los caracteres que en GET serian www.ninombre.com?valor1=valor1&valor2=valor2 osea como los paso cuando estoy trabajando en una pagina que use post

gracias
  #4 (permalink)  
Antiguo 24/08/2007, 14:05
Avatar de xknown  
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 20 años, 2 meses
Puntos: 7
Re: Codigo Post

Como te dije, la variable postData en el código que muestras tiene que ser más o menos así (lee el comentario anterior para el tratamiento de caracteres especiales):

string postData = "valor1=valor1&valor2=valor2";

Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:41.