Click here to Skip to main content
15,884,938 members
Home / Discussions / C#
   

C#

 
AnswerRe: How I can position a form? Pin
yyf2-Sep-03 11:31
yyf2-Sep-03 11:31 
GeneralDatabindings Pin
yyf2-Sep-03 10:44
yyf2-Sep-03 10:44 
GeneralRe: Databindings Pin
Gilles B.2-Sep-03 12:41
sussGilles B.2-Sep-03 12:41 
GeneralRe: Databindings Pin
yyf2-Sep-03 12:59
yyf2-Sep-03 12:59 
GeneralRe: Databindings Pin
Ista2-Sep-03 16:54
Ista2-Sep-03 16:54 
GeneralDataBindings Pin
yyf2-Sep-03 10:27
yyf2-Sep-03 10:27 
GeneralRe: DataBindings Pin
Ista2-Sep-03 16:56
Ista2-Sep-03 16:56 
GeneralAuthenticating to a web site Pin
roberthanacek2-Sep-03 10:24
roberthanacek2-Sep-03 10:24 
I am trying to automate login to a site for a subsequent file upload. Even though I set the Content-Length header I keep getting the 411 (length required) response error.

I begin with an initial unauthorized request to a form-authenticated page.
I extract the session cookie from the response header and attach it to the next request I send by POST method, along with the required authentication info. The program halts as soon as I try to access the next response, referring to the abovementioned error code.
Please tell me what I'm missing...

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
using System;
using System.Net;
using System.Web;
using System.Text;
using System.IO;

public class Loader {

private HttpWebRequest request;
private HttpWebResponse response;
private CookieCollection cookies;
private string url = "http://www.somedomain.com/member/home.jsp";
private string logPath = @"c:\\loader\response.log";
/*-------------------------------------------------------------------------*/
public static void Main() {

Loader loader = new Loader();

loader.MakeInitRequest();
loader.SendAuthInfo();

} // end Main
/*-------------------------------------------------------------------------*/
public void MakeInitRequest() {

request = (HttpWebRequest) WebRequest.Create(url);

// Set the request's CookieContainer
request.CookieContainer = new CookieContainer();

response = (HttpWebResponse) request.GetResponse();

// Capture cookies from the initial response
cookies = response.Cookies;

// Log the response
this.LogResponse("\n\n************INITIAL RESPONSE:************\n\n");

response.Close();

} // end MakeInitRequest
/*-------------------------------------------------------------------------*/
public void SendAuthInfo() {

string username = "my_username";
string password = "my_password";
string postData = "j_username=" + HttpUtility.UrlEncode(username) +
"&j_password=" + HttpUtility.UrlEncode(password);
byte[] postDataBytes = Encoding.ASCII.GetBytes(postData);

request = (HttpWebRequest) WebRequest.Create(url);

// Set the cookies from the initial response to this request
if (cookies != null) {
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
// Set the request headers
request.Method = "POST";
request.ContentType = "application-www-form-urlencoded";
request.ContentLength = postDataBytes.Length;

// Post authentication info
StreamWriter writer = new StreamWriter( request.GetRequestStream() );
writer.Write(postDataBytes);

// Log the response
response = (HttpWebResponse) request.GetResponse();
this.LogResponse("\n\n************NEXT RESPONSE************\n\n");

response.Close();
writer.Close();

} // end SendAuthInfo
/*-------------------------------------------------------------------------*/
private string DisplayResponseHeaders(WebHeaderCollection headers) {

StringBuilder buffer = new StringBuilder();

// Capture each header:value pair
foreach(string header in headers) {
buffer.Append(header + ": " + headers[header] + "\n");
}

return buffer.ToString();
} // end DisplayResponseHeaders
/*-------------------------------------------------------------------------*/
private void LogResponse(string title) {

FileStream fStream =
new FileStream(logPath, FileMode.Append, FileAccess.Write);
StreamWriter logWriter = new StreamWriter(fStream);
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString;

// Log the response
responseString =
this.DisplayResponseHeaders(response.Headers) +
reader.ReadToEnd();
logWriter.Write(title + responseString);

reader.Close();
logWriter.Close();
fStream.Close();

} // end LogResponse
/*-------------------------------------------------------------------------*/
} // end class Loader
GeneralRe: Authenticating to a web site Pin
leppie2-Sep-03 12:09
leppie2-Sep-03 12:09 
GeneralSingleCall Objects not Disposing Pin
0siris2-Sep-03 8:03
0siris2-Sep-03 8:03 
GeneralOverride the asterisk and row arrow in datagrid Pin
Ista2-Sep-03 7:02
Ista2-Sep-03 7:02 
General(Rich) Text Box and cursor position Pin
Frank Olorin Rizzi2-Sep-03 5:59
Frank Olorin Rizzi2-Sep-03 5:59 
GeneralRe: (Rich) Text Box and cursor position Pin
Frank Olorin Rizzi2-Sep-03 6:07
Frank Olorin Rizzi2-Sep-03 6:07 
GeneralRe: (Rich) Text Box and cursor position Pin
.gonad2-Sep-03 6:47
.gonad2-Sep-03 6:47 
QuestionCan you use functions like sprintf in c# Pin
IrishSonic2-Sep-03 5:59
IrishSonic2-Sep-03 5:59 
AnswerRe: Can you use functions like sprintf in c# Pin
flipdoubt2-Sep-03 7:04
flipdoubt2-Sep-03 7:04 
AnswerRe: Can you use functions like sprintf in c# Pin
leppie2-Sep-03 12:14
leppie2-Sep-03 12:14 
GeneralThanks, will check it out. Pin
IrishSonic2-Sep-03 22:06
IrishSonic2-Sep-03 22:06 
GeneralOffice Xp Task Panel Pin
Sanjay Rastogi2-Sep-03 3:15
Sanjay Rastogi2-Sep-03 3:15 
GeneralSingleCall and Threads Forever Pin
Member 4744652-Sep-03 2:32
Member 4744652-Sep-03 2:32 
GeneralWin32 API Wrapper Pin
vikramlinux1-Sep-03 23:34
vikramlinux1-Sep-03 23:34 
GeneralRe: Win32 API Wrapper Pin
J. Dunlap2-Sep-03 9:20
J. Dunlap2-Sep-03 9:20 
GeneralTrouble with 'set' accessor Pin
mikeirwin1-Sep-03 20:01
mikeirwin1-Sep-03 20:01 
GeneralRe: Trouble with 'set' accessor Pin
Ista2-Sep-03 7:01
Ista2-Sep-03 7:01 
GeneralRe: Trouble with 'set' accessor Pin
mikeirwin2-Sep-03 9:21
mikeirwin2-Sep-03 9:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.