需要C#post方法帮助 [英] C# post method help needed

查看:83
本文介绍了需要C#post方法帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我想将我的页面重定向到某个URL,需要ID和密码才能登录。

所以我想要在重定向时传递ID和密码。所以我没有登录窗口,直接我可以看到内容。



这是代码,

Hello All,

I want to redirect my page to some URL,which needs ID and password for login.
So i want to pass ID and Password while redirecting.So that i dont get login window and directly i can see the content.

Here is the code,

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
          HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
         request.Method = "POST";
          request.UseDefaultCredentials = false;
         request.PreAuthenticate = true;
         request.Credentials = new NetworkCredential("id", "pwd", "domain");
          HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
          this.Response.Write(response.StatusCode);
    }
    
    
        
}





请更正我的意思获取错误,因为服务器没有响应。



Please correct as i am getting error as no response from the server.

推荐答案

根据网站的不同,不会使用NetworkCredential。看看实际的登录表格,了解以下几点:



- 登录表格的动作是什么?

- 用户名字段的名称是什么?

- 密码字段的名称是什么?



然后使用该信息,您可以确保你发布到正确的URL,一些网站将登录请求发送到另一个页面。然后你可以将你的用户名和密码添加到请求的内容。



这样的东西:

假设UserName是的名字ID文本框和密码是密码文本框的名称。

Depending on the site the NetworkCredential wont be used. Take a look at the actual Log In form for a few things:

- What is the action of the Log In form?
- What is the name of the User Name field?
- What is the name of the Password field?

Then with that information you can make sure you POST to the right URL, some sites post log in requests to another page. Then you can add your User Name and Password to the content of the Request.

something like this:
Assuming UserName is the name of the ID textbox and Password is the name of the Password textbox.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
request.Method = "POST";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("id", "pwd", "domain");

string formData = "UserName=" + id + "&Password=" + pwd;
using (var writer = new StreamWriter(request.GetRequestStream()))
{
       writer.Write(formData);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
this.Response.Write(response.StatusCode);





此代码直接写入浏览器,因此可能无法编译。



This code was written directly into the browser so it probably wont compile.


这篇关于需要C#post方法帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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