我有一个调用MVC操作的jquery例程,该操作将对API URL进行PUT / POST。来自jQuery的调用很好,并且与使用C#调用API一样有效。当通过Firebug / Fiddler检查时,以JSON格式从API接收响应。
如何将该响应发送回调用jQuery?
我的C#代码是:
public string callAPIPut(string ApiUrl, string JsonString)
{
WebRequest request = WebRequest.Create(ApiUrl);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(JsonString);
request.ContentType = "application/json; charset=utf-8";
request.Method = WebRequestMethods.Http.Put;
request.ContentLength = JsonString.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, JsonString.Length);
newStream.Close();
return ""; // How do I return the JSON response from the API?
}
在进行GET时,我可以使用类似下面的内容将响应返回到调用jQuery:
response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
serviceResponse = sr.ReadToEnd();
}
return serviceResponse;
我不知道如何在执行Put / Post时返回响应?
答案 0 :(得分:3)
public ActionResult CallAPIPut(string ApiUrl, string JsonString)
{
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
byte[] data = Encoding.Default.GetBytes(JsonString);
byte[] result = client.UploadData(ApiUrl, "PUT", data);
return Content(Encoding.Default.GetString(result), "application/json");
}
}
或通过包装自定义和可重复使用的操作结果使其更智能,以避免使用基础架构管道混乱控制器:
public class ApiResult : ActionResult
{
public ApiResult(string apiUrl, string jsonData)
: this(apiUrl, jsonData, "PUT")
{
}
public ApiResult(string apiUrl, string jsonData, string method)
{
ApiUrl = apiUrl;
JsonData = jsonData;
Method = method;
}
public string ApiUrl { get; private set; }
public string JsonData { get; private set; }
public string Method { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
var contentType = "application/json";
response.ContentType = contentType;
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = contentType;
byte[] data = Encoding.Default.GetBytes(JsonData);
byte[] result = client.UploadData(ApiUrl, Method, data);
response.Write(Encoding.Default.GetString(result));
}
}
}
现在您的控制器操作变为:
public ActionResult CallAPIPut(string apiUrl, string jsonString)
{
return new ApiResult(apiUrl, jsonString);
}
答案 1 :(得分:0)
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, JsonString.Length);
newStream.Close();
您正在向服务器发布JSON。要获取JSON,您需要发布/放置并使用ResponseStream来读取服务器返回的数据。
样本:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Create a request for the URL.
WebRequest request = WebRequest.Create (
"http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream 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.
Console.WriteLine (responseFromServer);
// Clean up the streams and the response.
reader.Close ();
response.Close ();
}
}
}
来自http://msdn.microsoft.com/en-us/library/456dfw4f.aspx
的示例编辑:您将返回responseFromServer,并在您的Javascript回调中使用它。