本文介绍了在C#中的WebClient请求期间发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中的WebClient请求期间发生异常。当我运行项目时遇到错误。如何解决此错误?



var postdata =ratepleplan&1250&roomtype&254;



使用(WebClient客户端=新WebClient())

{



client.Headers [HttpRequestHeader.Accept] =* / *;

client.Headers [HttpRequestHeader.Referer] =https://dfddff.com/extranet/;

client.Headers [HttpRequestHeader.ContentType] =application / x-www-form-urlencoded; charset = UTF-8;

client.Headers [HttpRequestHeader.ContentLength] = postdata。 Length.ToString();

client.Headers [HttpRequestHeader.Cookie] = sessioncookie;

client.Headers [HttpRequestHeader.UserAgent] =Mozilla / 5.0(Windows NT 6.1) )AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 46.0.2490.86 Safari / 537.36 ;

//client.Headers.Add(HttpRequestHeader.Cookie,conssioncookie);

byte [] postArray = Encoding.ASCII.GetBytes(postdata);

byte [] response = client.UploadData(https://dfddff.com/rate,POST,postArray); ==========>得到错误

var responsehtml = Encoding.UTF8.GetString(response);

}

An exception occurred during a WebClient request in C#.when i run project got above error.How to solve this error?

var postdata = "ratepleplan&1250&roomtype&254";

using (WebClient client = new WebClient())
{

client.Headers[HttpRequestHeader.Accept] = "*/*";
client.Headers[HttpRequestHeader.Referer] = "https://dfddff.com/extranet/";
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
client.Headers[HttpRequestHeader.ContentLength] = postdata.Length.ToString();
client.Headers[HttpRequestHeader.Cookie] = sessioncookie;
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36";
//client.Headers.Add(HttpRequestHeader.Cookie, sessioncookie);
byte[] postArray = Encoding.ASCII.GetBytes(postdata);
byte[] response = client.UploadData("https://dfddff.com/rate", "POST", postArray); ==========> Got error
var responsehtml = Encoding.UTF8.GetString(response);
}

推荐答案

var postdata = "ratepleplan&1250&roomtype&254";





这是无效的发布数据,必须是喜欢这个





That's not valid post data, it has to be like this

var postdata = "ratepleplan=1250&roomtype=254";


这篇关于在C#中的WebClient请求期间发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:30