html get方式传字节流,关于HttpWebResponse,写一个post方法,必须先将 ContentLength 字节写入请求流,...

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net;

using System.IO;

namespace TestCon

{

static class BillInfo

{

public static string BillInfo_Post_Get(string Post_String,string Post_Data) {

try {

string s_Connection_String = Post_String;

HttpWebRequest BillInfo_Request = (HttpWebRequest)WebRequest.Create(s_Connection_String);

BillInfo_Request.Method = "POST";

BillInfo_Request.ContentType = "application/x-www-form-urlencoded";

BillInfo_Request.ContentLength = s_Connection_String.Length;

BillInfo_Request.Timeout = 20000;

Stream BillInfo_Stream = BillInfo_Request.GetRequestStream();

BillInfo_Stream.Write(Encoding.UTF8.GetBytes(Post_Data), 0, Encoding.UTF8.GetBytes(Post_Data).Length);

HttpWebResponse BillInfo_Response = (HttpWebResponse)BillInfo_Request.GetResponse();

StreamReader BillInfo_Reader = new StreamReader(BillInfo_Response.GetResponseStream(), Encoding.UTF8);

return BillInfo_Reader.ReadToEnd();

}

catch (System.Exception ex) {

Console.WriteLine(ex);

Console.ReadLine();

return "";

}

}

}

}

解决方案

10

不要设置ContentLength

20

引用:

Quote: 引用:

不要设置ContentLength

问题解决了= =但是本人还是很好奇为什么不能设置这个,本人设置的长度和本人的字段长度是一致的呀

一致个屁,你写的长度和设置的长度方法用的都不一样

10

关键现在http协议规定必须有contentlength,所以就算你不设置,系统也会帮你加上,根本不需要关心这个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值