简体   繁体   English

使用C#HttpWebRequest将json发送到Web服务

[英]Use C# HttpWebRequest to send json to web service

I am new to JSON and need help. 我是JSON的新手,需要帮助。 I have some JSON working in jquery and get the information back correctly from the web service I have running on the web. 我在jquery中运行了一些JSON,并从我在Web上运行的Web服务中正确地获取信息。 However, I can't get it to work using HttpWebRequest in C#. 但是,我无法在C#中使用HttpWebRequest来使用它。 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, 这是在Web服务上,我希望能够使用此代码调用“callJson(string x)”,

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, 当我将类型更改为application / json并添加时,

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. 首先,您错过了要在webservice中添加的ScriptService属性。

[ScriptService] [ScriptService]

After then try following method to call webservice via JSON. 然后尝试以下方法通过JSON调用webservice。

  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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM