如何使用httpwebrequest post方法传递JSON数据。 [英] How to pass JSON data using httpwebrequest post method.

查看:1964
本文介绍了如何使用httpwebrequest post方法传递JSON数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想使用httpwebrequest POST方法传递JSON数据。 


JSON类:

 public class CreatePerson 
{
public string name {get;组; }
public string owner_id {get;组; }
public string email {get;组; }
}

我想传递这个值。我从我的代码中检索这个值。


webservice:

 public void CreatePerson(string item1,string item2)
{
var httpWebRequest =(HttpWebRequest)WebRequest.Create(" url");
httpWebRequest.ContentType =" application / json" ;;
httpWebRequest.Method =" POST" ;;
using(var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json =" {\" name \":\"" + item2 +" \"," +
" \" email \":\"" + item1 +" \"}" ;;
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}

var httpResponse =(HttpWebResponse)httpWebRequest.GetResponse();
using(var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}




解决方案

嗨riya25,


感谢您在此发帖。


为您服务问题你可以参考下面的链接。


https://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server


如果您的问题与ASP更相关.NET,您可以在
ASP.NET论坛
中发布新主题以获得合适的支持。


CLR论坛讨论并提出有关.NET Framework基类(BCL)的问题,例如集合,I / O,Regigistry,Globalization,Reflection。还讨论了构建或扩展.NET Framework的所有其他Microsoft库,包括Managed
可扩展性框架(MEF),图表控件,CardSpace,Windows Identity Foundation(WIF),销售点(POS),事务处理。 


最诚挚的问候,


Wendy


Hi,

I want to pass JSON data using httpwebrequest POST method. 

JSON class:

public class CreatePerson
{
    public string name { get; set; }
    public string owner_id { get; set; }
    public string email { get; set; }
}

I want to pass this values.This values I am retrieving from my code.

webservice:

public  void CreatePerson(string item1 ,string item2)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("url");          
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"name\":\"" + item2 + "\"," +
           "\"email\":\"" + item1 + "\"}";
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }


解决方案

Hi riya25,

Thank you for posting here.

For your question you could refer to the link below.

https://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server

If your question is more related to ASP.NET, you could post a new thread in ASP.NET forum for suitable support.

The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions. 

Best Regards,

Wendy


这篇关于如何使用httpwebrequest post方法传递JSON数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆