Click here to Skip to main content
11,709,529 members (45,074 online)
   

C#

 
GeneralRe: Reflection with args.Lenght =0 Pin
PIEBALDconsult21-Jan-13 4:47
memberPIEBALDconsult21-Jan-13 4:47 
AnswerRe: Reflection with args.Lenght =0 Pin
Alan N20-Jan-13 22:37
memberAlan N20-Jan-13 22:37 
QuestionNeed some guide line for few tools which is used for dotnet. Pin
Tridip Bhattacharjee20-Jan-13 8:59
memberTridip Bhattacharjee20-Jan-13 8:59 
AnswerRe: Need some guide line for few tools which is used for dotnet. Pin
Super Lloyd20-Jan-13 12:40
memberSuper Lloyd20-Jan-13 12:40 
Questionconnect to usb modem Pin
hafez20-Jan-13 0:36
memberhafez20-Jan-13 0:36 
AnswerRe: connect to usb modem Pin
Andy41121-Jan-13 3:40
memberAndy41121-Jan-13 3:40 
AnswerRe: connect to usb modem Pin
Cl.Kurtz8-Feb-13 15:24
memberCl.Kurtz8-Feb-13 15:24 
QuestionWho can help me to solve this problem ? Pin
905607625@qq.com19-Jan-13 15:32
member905607625@qq.com19-Jan-13 15:32 
A 401 error occur when i use post method to login a website.

The website is: www.19lou.com/login

My username is: cyberarmy
My password is: wangyouliang


My C# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
 
namespace _19lou
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        const string LoginUrl = @"http://www.19lou.com/login";
        HttpWebRequest httpWebRequest;
        HttpWebResponse httpWebResponse;
        string responseFromServer;              //SendLogin返回的响应流
        string username = "cyberarmy"; //username
        string password = "wangyouliang"; //password
        private void btn_login_Click(object sender, EventArgs e)
        {
            if (txt_username.Text != "" && txt_password.Text != "")
            {
               // username = txt_username.Text;
               // password = txt_password.Text;
                string PostData = string.Format("userName={0}&userPass={1}&captcha=&checked=0&cityInfo=&refererUrl=aHR0cDovL3d3dy4xOWxvdS5jb20=&remember=1", username, password);
                SendLogin(LoginUrl, PostData);
            }
        }
 

 
        public void SendLogin(string loginUrl, string postData)
        {
            byte[] byteArray = Encoding.GetEncoding("GBK").GetBytes(postData);
            try
            {
                //基于apache服务器,IIS发布的则不需要  
                ServicePointManager.Expect100Continue = false;
                CookieContainer cookieContainer = new CookieContainer();
                //创建对url的请求  

                httpWebRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
                httpWebRequest.Credentials = new NetworkCredential(username, password, "www.19lou.com");
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.Headers["Accept-Language"] = "zh-cn";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                //协议方式  
                httpWebRequest.Method = "POST";
                //post开始  
                //请求内容长度  
                httpWebRequest.ContentLength = byteArray.Length;
                Stream dataStream = httpWebRequest.GetRequestStream();
                // 请求数据放入请求流  
                dataStream.Write(byteArray, 0, byteArray.Length);
 
                //返回html  
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    //读取响应流  
                    responseFromServer = reader.ReadToEnd();
                    //MessageBox.Show(responseFromServer);    //读响应流html
                    reader.Close();
                    dataStream.Close();
                    httpWebResponse.Close();
                }
            }
 
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

AnswerRe: Who can help me to solve this problem ? Pin
Dave Kreskowiak19-Jan-13 15:34
mvpDave Kreskowiak19-Jan-13 15:34 
GeneralRe: Who can help me to solve this problem ? Pin
905607625@qq.com19-Jan-13 15:40
member905607625@qq.com19-Jan-13 15:40 
GeneralRe: Who can help me to solve this problem ? Pin
Dave Kreskowiak19-Jan-13 15:47
mvpDave Kreskowiak19-Jan-13 15:47 
GeneralRe: Who can help me to solve this problem ? Pin
Richard MacCutchan19-Jan-13 21:34
mvpRichard MacCutchan19-Jan-13 21:34 
GeneralRe: Who can help me to solve this problem ? Pin
BobJanova21-Jan-13 5:47
memberBobJanova21-Jan-13 5:47 
AnswerRe: Who can help me to solve this problem ? Pin
Eddy Vluggen20-Jan-13 2:24
mvpEddy Vluggen20-Jan-13 2:24 
GeneralRe: Who can help me to solve this problem ? Pin
Dave Kreskowiak20-Jan-13 6:12
mvpDave Kreskowiak20-Jan-13 6:12 
Questioncollections generic with wcf Pin
yonim12319-Jan-13 12:33
memberyonim12319-Jan-13 12:33 
AnswerRe: collections generic with wcf Pin
SledgeHammer0119-Jan-13 13:03
memberSledgeHammer0119-Jan-13 13:03 
GeneralRe: collections generic with wcf Pin
yonim12319-Jan-13 13:20
memberyonim12319-Jan-13 13:20 
QuestionCannot Access the form _ Cross-thread operation Pin
mohammadkaab19-Jan-13 9:50
membermohammadkaab19-Jan-13 9:50 
AnswerRe: Cannot Access the form _ Cross-thread operation Pin
jibesh19-Jan-13 21:29
memberjibesh19-Jan-13 21:29 
AnswerRe: Cannot Access the form _ Cross-thread operation Pin
pt140120-Jan-13 22:13
memberpt140120-Jan-13 22:13 
GeneralRe: Cannot Access the form _ Cross-thread operation Pin
mohammadkaab21-Jan-13 20:26
membermohammadkaab21-Jan-13 20:26 
Questionwcf p2p file transfering over internet Pin
vab26108519-Jan-13 2:03
membervab26108519-Jan-13 2:03 
QuestionWorking with tab escape charecter Pin
Mc_Topaz19-Jan-13 1:49
memberMc_Topaz19-Jan-13 1:49 
AnswerRe: Working with tab escape charecter Pin
Abhinav S19-Jan-13 2:38
mvpAbhinav S19-Jan-13 2:38 

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

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


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 31 Aug 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid