Click here to Skip to main content
15,036,363 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

C#
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();


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
Posted
Comments
Christian Graus 29-Jul-12 16:39pm
   
This object does work, I use it all the time. Stick with one example, read the documentation and work through it, instead of searching for a random snippet that does everything you want it to.

1 solution

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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900