您好我正在尝试使用tcpclient类发布到登录页面。但它说302错误。我可以用webclient做同样的事情。请建议我遗漏的地方。 我已经实现了使用webclient类。但我要求在较低级别做。请建议。
我试过
string sPost = "POST http://www.udupikrishnarestaurant.com/ HTTP/1.1\r\n" +
"Host: www.udupikrishnarestaurant.com\r\n" +
"Connection: keep-alive\r\n" +
"Content-Length: 632\r\n" +
"Cache-Control: max-age=0\r\n" +
"Origin: http://www.udupikrishnarestaurant.com \r\n" +
"Upgrade-Insecure-Requests: 1\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 \r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 \r\n" +
"Referer: http://www.udupikrishnarestaurant.com/ \r\n" +
"Accept-Encoding: gzip, deflate, br\r\n" +
"Accept-Language: en-US,en;q=0.8\r\n" +
"\r\n" +
"__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=/wEPDwUKLTM0ODY4NTA5Mg9kFgRmD2QWAgIBDxYCHgdWaXNpYmxlaGQCAQ8WAh4EVGV4dAUOMTgyLjY5LjEwOC4yMTFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBQlidG5TdWJtaXQFCWJ0bkZvcmdvdCvBSicNzlFc1g4UTZeO96HhGK8T&__VIEWSTATEGENERATOR=CA0B0334&__EVENTVALIDATION=/wEdAAr0HIr+WBkxh8yUckvqwLD4LqRynaTFa/5Hs7sApa3/2aRDGrH080vTDzQFiILKSswLyPw09K59GBb2rr9Ouzg352npSE7sRexHKIcGFqd4MCl0Pj8zqX34hAcL3lPJ6TZjemWCTRgEB59HPczIGVNwdjb4xzpKucCqKjegIpvFhjzmltaUM7aEAN+g9cP/m13MQ92x/wmKgUDwu+2MEZnSA5KkNGEzQJYQmFWkBWmYqAdLpnM=&ddlFloor=2&txtUserName=test&txtPassword=1234&btnSubmit.x=15&btnSubmit.y=3";
byte[] buf = new byte[1024];
string header = sPost;
var client = new TcpClient();
client.Connect("182.50.132.48", 80);
// send request
var stream = client.GetStream();
var streamReader = new StreamReader(stream);
var streamWriter = new StreamWriter(stream);
streamWriter.Write(header);
streamWriter.Flush();
stream.Flush();
// get response
var response1 = streamReader.ReadLine();
答案 0 :(得分:0)
302不是错误。这是一个重定向请求 - 表单告诉你“转到那里”,很可能是你登录尝试的结果。 WebClient
处理重定向,而客户端则没有。
要实现很多HTTP。除非这纯粹是一种练习,否则不要尝试编写自己的HTTP客户端。重定向只是冰山一角。