httpWebRequest怎么POST,JSON数据

httpWebRequest怎么POST,JSON数据,第1张

/// <summary>

/// 返仔竖回JSon数据

/// </summary>

/// <param name="JSONData">要处理的JSON数据</param>

/// <param name="Url">要提交的URL<睁戚盯/param>

/// <returns>返回的JSON处理字符串</returns>

public string GetResponseData(string JSONData,string Url)

{byte[] bytes = Encoding.UTF8.GetBytes(JSONData)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url)

request.Method = "POST"

request.ContentLength = bytes.Length

request.ContentType = "text/xml"

Stream reqstream = request.GetRequestStream()

reqstream.Write(bytes, 0, bytes.Length)

//声明一个HttpWebRequest请求

request.Timeout = 90000

//设悉和置连接超时时间

request.Headers.Set("Pragma", "no-cache")

HttpWebResponse response = (HttpWebResponse)request.GetResponse()

Stream streamReceive = response.GetResponseStream()

Encoding encoding = Encoding.UTF8

StreamReader streamReader = new StreamReader(streamReceive, encoding)

string strResult = streamReader.ReadToEnd()

streamReceive.Dispose()

streamReader.Dispose()

return strResult

}

如果不通过request.write()的方式向接口post数据,将request.ContentLength属性设置为0,让后去掉request.write()方法即可!

以前玩网页游戏我也弄过HttpWebRequest来挂机,大概如下,

HttpWebRequest _httpWebRequest = HttpWebRequest.Create("网址")

_httpWebRequest.Method = "POST" //设置为post方法尘隐

// SetHeaders() 设置request的headers

_postDataBytes = _postDataEncoding.GetBytes(_postData)

//将Post的信息按照 _postDataEncoding 字符编码方式进行转换得到字节数组。

_httpWebRequest.ContentLength = _postDataBytes.Length

//设置request的Content-Length

System.IO.Stream s = _httpWebRequest.GetRequestStream()

s.Write(_postDataBytes, 0, _postDataBytes.Length)

//将post信息写入request流

s.Close()

_httpWebRequest.GetResponse()

我是这么弄得,没问题。包括进行差卜登陆那些都可以。

还有,一般网站的登陆按钮点虚兄穗了后,都会有不止一个POST或GET,建议把那些都实现。

string postdata = "sort=xxxxx&type=xxxxxx"洞判

byte[] 纳亩改bytes = Encoding.ASCII.GetBytes(postData)

Stream sendStream = request.GetRequestStream()

sendStream.Write(bytes, 0, 耐誉bytes.Length)

sendStream.Close()

HttpWebResponse response = (HttpWebResponse)request.GetResponse()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8171598.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存