2010-11-23 15 views

Respuesta

85

Con HttpWebRequest.GetRequestStream ejemplo

Código de http://msdn.microsoft.com/en-us/library/d4cek6cc.aspx

string postData = "firstone=" + inputData; 
ASCIIEncoding encoding = new ASCIIEncoding(); 
byte[] byte1 = encoding.GetBytes (postData); 

// Set the content type of the data being posted. 
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 

// Set the content length of the string being posted. 
myHttpWebRequest.ContentLength = byte1.Length; 

Stream newStream = myHttpWebRequest.GetRequestStream(); 

newStream.Write (byte1, 0, byte1.Length); 

De uno de mi propio código:

var request = (HttpWebRequest)WebRequest.Create(uri); 
request.Credentials = this.credentials; 
request.Method = method; 
request.ContentType = "application/atom+xml;type=entry"; 
using (Stream requestStream = request.GetRequestStream()) 
using (var xmlWriter = XmlWriter.Create(requestStream, new XmlWriterSettings() { Indent = true, NewLineHandling = NewLineHandling.Entitize, })) 
{ 
    cmisAtomEntry.WriteXml(xmlWriter); 
} 

try 
{  
    return (HttpWebResponse)request.GetResponse(); 
} 
catch (WebException wex) 
{ 
    var httpResponse = wex.Response as HttpWebResponse; 
    if (httpResponse != null) 
    { 
     throw new ApplicationException(string.Format(
      "Remote server call {0} {1} resulted in a http error {2} {3}.", 
      method, 
      uri, 
      httpResponse.StatusCode, 
      httpResponse.StatusDescription), wex); 
    } 
    else 
    { 
     throw new ApplicationException(string.Format(
      "Remote server call {0} {1} resulted in an error.", 
      method, 
      uri), wex); 
    } 
} 
catch (Exception) 
{ 
    throw; 
} 
+0

Hola, Torbjorn, estoy usando la solicitud para poder obtener la 'request.GetResponse();', en el ejemplo anterior, ¿cómo funcionaría? –

+0

Cuando llama a GetRequestStream() realiza la llamada al servidor. Entonces, tendrías que agregar eso al final del ejemplo anterior. –

+0

¿Hay alguna forma de ver el texto completo dentro de un objeto de solicitud para depuración? Intenté serializarlo e intenté usar StreamReader, pero no importa lo que haga, no puedo ver los datos que acabo de escribir en la solicitud. – James

34

Esto debería ayudar:

var request = (HttpWebRequest)WebRequest.Create("http://example.com/page.asp"); 

string stringData = ""; //place body here 
var data = Encoding.ASCII.GetBytes(stringData); // or UTF8 

request.Method = "PUT"; 
request.ContentType = ""; //place MIME type here 
request.ContentLength = data.Length; 

var newStream = request.GetRequestStream(); 
newStream.Write(data, 0, data.Length); 
newStream.Close(); 
+0

¿Echas en falta algo? Como un httpWReq.Content = newStream; no está utilizando su nuevo objeto Stream con su webRequest. – Yogurtu

+2

Para responder a la pregunta de @ Yogurtu sobre la completitud, el objeto 'Stream' que' newStream' apunta escribe directamente al cuerpo de la solicitud. Se accede mediante la llamada a 'HttpWReq.GetRequestStream()'. No hay necesidad de establecer nada más en la solicitud. – MojoFilter

Cuestiones relacionadas