使用C#HttpWebRequest的,以JSON发送到Web服务 [英] Use C# HttpWebRequest to send json to web service
问题描述
我是新来的JSON和需要帮助。我有一些JSON jQuery的工作和我已在网页上运行的Web服务得到正确的信息回来。但是,我无法得到它在C#中使用HttpWebRequest的工作。我将张贴下面的代码
///<总结>
///概要描述VBRService
///< /总结>
[WebService的空间(namespace =http://test.visitblueridge.com/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(假) ]
//要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类VBRService:System.Web.Services.WebService
{
[的WebMethod]
公共字符串的HelloWorld( )
{
返回的Hello World;
}
[的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
公共字符串callJson(字符串x)
{
返回曾为=+ X;
}
}
这是对Web服务,我想成为能够称之为callJson(字符串x)使用此代码,
VAR的HttpWebRequest =(HttpWebRequest的)WebRequest.Create(webAddr) ;
httpWebRequest.ContentType =文/ JSON;
httpWebRequest.Method =POST;
使用(VAR的StreamWriter =新的StreamWriter(httpWebRequest.GetRequestStream()))
{
JSON字符串={\x\:\true\\ \\};
streamWriter.Write(JSON);
streamWriter.Flush();
}
VAR的HttpResponse =(HttpWebResponse)httpWebRequest.GetResponse();
使用(VAR的StreamReader =新的StreamReader(httpResponse.GetResponseStream()))
{
VAR的结果= streamReader.ReadToEnd();
返回结果;
}
我不断收到内部服务器错误。当我更改类型应用程序/ JSON和添加,
request.Headers.Add(SOAPAction报,HTTP:/ /test.visitblueridge.com/callJson);
我得到一个不被接受媒体错误。
谢谢你在前进,并希望这可以帮助别人。
首先,你错过了ScriptService属性在web服务添加所有的。
[ScriptService]
块引用>
在此之后尝试以下方法通过JSON调用Web服务
VAR webAddr =HTTP://域名/ VBRService。 ASMX / callJson
VAR的HttpWebRequest =(HttpWebRequest的)WebRequest.Create(webAddr);
httpWebRequest.ContentType =应用/ JSON的;字符集= UTF-8;
httpWebRequest.Method =POST;
使用(VAR的StreamWriter =新的StreamWriter(httpWebRequest.GetRequestStream()))
{
JSON字符串={\x\:\true\\ \\};
streamWriter.Write(JSON);
streamWriter.Flush();
}
VAR的HttpResponse =(HttpWebResponse)httpWebRequest.GetResponse();
使用(VAR的StreamReader =新的StreamReader(httpResponse.GetResponseStream()))
{
VAR的结果= streamReader.ReadToEnd();
返回结果;
}
块引用>I am new to JSON and need help. I have some JSON working in jquery and get the information back correctly from the web service I have running on the web. However, I can't get it to work using HttpWebRequest in C#. I will post the code below.
/// <summary> /// Summary description for VBRService /// </summary> [WebService(Namespace = "http://test.visitblueridge.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class VBRService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string callJson(string x) { return "Worked =" + x; } }
That is on the web service and I want to be able to call "callJson(string x)" using this code,
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = "{\"x\":\"true\"}"; streamWriter.Write(json); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); return result; }
I keep getting an internal server error. When I change the type to application/json and add,
request.Headers.Add("SOAPAction", "http://test.visitblueridge.com/callJson");
I get an unaccepted media error.
Thank you in advance and hope this helps others.
解决方案First of all you missed ScriptService attribute to add in webservice.
[ScriptService]
After then try following method to call webservice via JSON.
var webAddr = "http://Domain/VBRService.asmx/callJson"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json; charset=utf-8"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = "{\"x\":\"true\"}"; streamWriter.Write(json); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); return result; }
这篇关于使用C#HttpWebRequest的,以JSON发送到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!