从桌面应用程序发送Web请求 [英] send web request from desktop application

查看:136
本文介绍了从桌面应用程序发送Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接到网站并向其发送一些参数

我搜索了一段时间,找到了有关http Web请求对象的文章,并尝试了一下,但没有成功

我到达的最后一个代码是:

I need to connect to a website and send it some parameters

I searched for it for a while and found articles about the http web request object and tried it but it didn''t work

the last code i reached was:

Uri uri = new Uri("https://www.hanjin.com/hanjin/CUP_HOM_3321.do?redir=Y&sessLocale=en");
string PostData = "bkgNo=" + txt_BOL.Text;
string proxy = null;
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(PostData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.AllowWriteStreamBuffering = true;
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
request.Credentials = CredentialCache.DefaultCredentials;
request.UseDefaultCredentials = true;
request.ProtocolVersion = HttpVersion.Version10;
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.ContentLength = bytes.Length;
request.ContentType = "text/html;charset=UTF-8";
request.Proxy = new System.Net.WebProxy(proxy, true);
request.Accept = "*/*";
System.IO.Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Flush();
dataStream.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
long y = response.ContentLength;
string str = response.ContentType.ToString();
System.IO.Stream resst = response.GetResponseStream(); // display HTTP response
System.IO.StreamReader sr = new System.IO.StreamReader(resst);
string result = sr.ReadToEnd();
sr.Close();
resst.Close();
response.Close();



但它从来没有奏效

且响应内容长度始终为-1

谁能告诉我(内容长度= -1)是什么意思?
以及为什么代码不起作用请?????

谢谢您的帮助.



but it never worked

and the reponse content length always is -1

can any one tell me what (respomse content length = -1) means ??
and why the code doesn''t work please ??

thank you for your help

推荐答案

此-1在文档中有很好的解释:
http://msdn.microsoft.com/en-us/library/system. net.httpwebresponse.contentlength.aspx [ ^ ].

检查您的HTTP请求,URL和其他参数,在肯定有用的其他URL上测试代码...

—SA
This -1 is well explained in documentation:
http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.contentlength.aspx[^].

Check up your HTTP request, URL and other parameters, test your code on some other URL which certainly works…

—SA


这篇关于从桌面应用程序发送Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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