c#中的HttpWebRequest身份验证 [英] HttpWebRequest Authentication in c#

查看:877
本文介绍了c#中的HttpWebRequest身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在下面提到的课程中有http post方法



we have http post method in class mention below

public static class Http
        {
            public static byte[] Post(string uri, NameValueCollection pairs)
            {
                byte[] response = null;
                using (WebClient client = new WebClient())
                {
                    response = client.UploadValues(uri, pairs);
                }
            return response;
            }
        }   



我们正在登录但无法登录,

请参阅post方法和其参数如下所述。




we are tyring to login but not able to login,
please see the post method and its parameter mention below.

var response = Http.Post("https://ca-test.com/login.shtml", new     NameValueCollection() {
       { "j_account", "XXXX" },
       { "j_username", "XXXX" },
       { "j_password", "XXXX" },
   });

推荐答案





你发送证件在http标头?如果是真的请按照下面的代码。



public void PostMethod()

{

try

{

string svcCred =credentials;



string textJSONdata = System.IO.File.ReadAllText(@ D:\ data \ _jsontext.txt);



DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string));

MemoryStream mem = new MemoryStream();

ser.WriteObject(mem,textJSONdata);

string data = Encoding.UTF8.GetString(mem.ToArray(),0,( int)mem.Length);



WebClient webClient = new WebClient();

webClient.Headers [Content-type] = application / json;

webClient.Headers.Add(授权,svcCred);

webClient.Encoding = Encoding.UTF8;

webClient.UploadString(@http://testURL.com/Service.svc/UpdateGenformQuestions,POST,数据);



}

catch(exception ex)

{

Console.WriteLine(ex.ToString());

}





}
Hi,

are you sending credentials in http headers? if its true please follow below code.

public void PostMethod()
{
try
{
string svcCred = "credentials";

string textJSONdata = System.IO.File.ReadAllText(@"D:\data\jsontext.txt");

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(string));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, textJSONdata);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);

WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Headers.Add("Authorization", svcCred);
webClient.Encoding = Encoding.UTF8;
webClient.UploadString(@"http://testURL.com/Service.svc/UpdateGenformQuestions", "POST", data);

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}


}


这篇关于c#中的HttpWebRequest身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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