返回要在页面asp.mvc中呈现的http响应

时间:2015-08-10 10:03:54

标签: c# asp.net-mvc http

我想调用一个post请求,它会返回一个应该由浏览器呈现的http响应。我的尝试导致浏览器显示文本值system.web.httpwebresponse

var webrequest = WebRequest.Create("URL");
webrequest.Method = "POST";
string postData = "test=1&test2=2";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.ContentLength = byteArray.Length;
Stream dataStream = webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length); WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://proxy");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the 
myProxy.Credentials = new NetworkCredential("user", "pass");
webrequest.Proxy = myProxy;
var resp = webrequest.GetResponse();
return resp;

2 个答案:

答案 0 :(得分:1)

如果你的结果是文字,那么你可以

return Content(new StreamReader(resp.GetResponseStream()).ReadToEnd(), resp.ContentType)

答案 1 :(得分:1)

您可以将该流作为File响应返回。

return File(resp.GetResponseStream(), resp.ContentType);