Advanced Minds

  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • red color
  • green color
FireBoard
Welcome, Guest
Please Login or Register.    Lost Password?
url string length Programmatic construction of httprequest? (1 viewing) (1) Guests
Go to bottom Post Reply Favoured: 0
TOPIC: url string length Programmatic construction of httprequest?
#4502
Joerg Jooss (Visitor)
Click here to see the profile of this user
Birthdate:
url string length Programmatic construction of httprequest?  
Joerg   I assumed that you hadn't run this code since it contains an error that won't let it compile; i.e., GetByte should be GetBytes in the _expression_ defining the content variable. Ouch, you're right... I just grabbed the code from one of my earlier posts... I'm sorry. BTW, here's working sample with URL-encoding: public void PostForm(string url, string formData, string encodingName) {   MemoryStream content = new MemoryStream(formData.Length * 2);   Encoding encoding = Encoding.GetEncoding(encodingName);   string[] keyValuePairs = formData.Split('&', '=');   int numberOfPairs = keyValuePairs.Length;   bool isKey = true;   foreach (string keyValue in keyValuePairs) {     byte[] bytes = HttpUtility.UrlEncodeToBytes(keyValue, encoding);     content.Write(bytes, 0, bytes.Length);     if (isKey) {       content.WriteByte((byte) '=');     }     else {       content.WriteByte((byte) '&');     }     isKey = !isKey;   }   content.SetLength(content.Length - 1);   HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);   request.Method = POST ;   request.ContentType = String.Format(     application/x-www-form-urlencoded; charset={0} , encodingName);;   request.ContentLength = content.Length;   using (Stream requestStream = request.GetRequestStream()) {     content.WriteTo(requestStream);   }   HttpWebResponse response = (HttpWebResponse) request.GetResponse();   using (Stream responseStream = response.GetResponseStream()) {     byte[] buffer = new byte[response.ContentLength];     responseStream.Read(buffer, 0, buffer.Length);     // use buffer...   } } Cheers,
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#4503
Bill Cohagan (Visitor)
Click here to see the profile of this user
Birthdate:
url string length Programmatic construction of httprequest?  
Joerg   I tried the code you supplied, but I'm getting the same error. More exactly here's the code I used: =================== public void PostForm(string url, string formData, string encoding) { byte[] content = Encoding.GetEncoding(encoding).GetBytes(formData); string contentType = String.Format( application/x-www-form-urlencoded; charset={0} , encoding); HttpWebRequest request = (HttpWebRequest) System.Net.WebRequest.Create(url); request.Method = POST ; request.ContentType = contentType; request.ContentLength = content.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(content, 0, content.Length); } HttpWebResponse response = (HttpWebResponse) request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { byte[] buffer = new byte[response.ContentLength]; responseStream.Read(buffer, 0, buffer.Length); // Do something useful with buffer } } ================= The code that calls your method is; PostForm( http://localhost/QW/spike1.aspx , foo=bar , utf-8 ); I get the same error(The remote server returned an error: (500) Internal Server Error.) at the line that calls GetResponse(). Any suggestions? Oops, it seems I missed the fact that you get a 500
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop
authorization failed sprawdz autoryzacje nieautoryzowano 905 nieautoryzowano
car loans covering news eharmony codes tours south america world news