相关疑难解决方法(0)

HttpWebRequest使用基本身份验证

我正在尝试通过一个身份验证请求来模仿我们习惯在为此行为设置IIS时看到的"基本身份验证请求".

URL为:https
://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC =22&SC=1&ST=2(警告:https!)

此服务器在UNIX和Java下作为应用程序服务器运行.

这是我用来连接到此服务器的代码:

CookieContainer myContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2");
request.Credentials = new NetworkCredential(xxx,xxx);
request.CookieContainer = myContainer;
request.PreAuthenticate = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)

(我从本网站上的另一篇文章中复制了此内容).但我从服务器收到这个答案:

底层连接已关闭:发送时发生意外错误.

我想我尝试了所有可能的任务,我对C#的知识必须提供给我,但没有...

c# authentication credentials webrequest

130
推荐指数
6
解决办法
28万
查看次数

HttpWebRequest不传递凭据

我正在尝试使用HTTPWebRequest访问REST服务,并且在传递凭据时遇到问题,请参阅下面的代码.我读过它NetworkCredential不支持SSL,而且我正在访问HTTPS站点.有没有人知道类似于NetworkCredential支持SSL的类?

Uri requestUri = null;
Uri.TryCreate("https://mywebserver/webpage", UriKind.Absolute, out requestUri);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
NetworkCredential nc = new NetworkCredential("user", "password");
request.Credentials = nc;
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)

c# asp.net httpwebrequest

32
推荐指数
3
解决办法
5万
查看次数

为什么我的Http客户端在指定凭据时发出2个请求?

我创建了RESTful webservice(WCF),我检查每个请求的凭据.我的一个客户是Android应用程序,服务器端的一切似乎都很棒.我收到请求,如果它有正确的标题 - 我处理它等.

现在我创建了使用此服务的客户端应用程序.这是我做GET的方式:

// Create the web request  
            var request = WebRequest.Create(Context.ServiceURL + uri) as HttpWebRequest;

            if (request != null)
            {
                request.ContentType = "application/json";

                // Add authentication to request  
                request.Credentials = new NetworkCredential(Context.UserName, Context.Password);

                // Get response  
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    // Get the response stream  
                    if (response != null)
                    {
                        var reader = new StreamReader(response.GetResponseStream());

                        // Console application output
                        var s = reader.ReadToEnd();

                        var serializer = new JavaScriptSerializer();
                        var returnValue = (T)serializer.Deserialize(s, typeof(T)); …
Run Code Online (Sandbox Code Playgroud)

c# http httpwebrequest basic-authentication

11
推荐指数
2
解决办法
5260
查看次数

使用 HttpClient 的随机 401 未授权错误

我正在使用 HttpClient 调用 Web Api REST 端点,偶尔我会看到一个随机的 401 未授权状态。

这是我的 HttpClient 包装器,它是为可重用性目的而创建的。我已经修改了它以保持这篇文章的简单而不修改核心部分。

public class HttpHelper
{
        public const string JsonContentType = "application/json";

        public T PostAsJsonObject<T>(string uri, T t) where T : class
        {
            using (HttpResponseMessage response = PostJsonHttpRequest(uri, JsonContentType, t))
            {
                response.EnsureSuccessStatusCode();
                return response.Content.ReadAsAsync<T>().Result;
            }
        }

        private HttpResponseMessage PostJsonHttpRequest<T>(string uri, string contentType, T content) where T : class
        {
            if (String.IsNullOrEmpty(uri))
            {
                throw new ArgumentNullException("uri");
            }
            HttpResponseMessage response = CreateHttpClient(contentType).PostAsJsonAsync(uri, content).Result;
            return response;
        }

        private HttpClient CreateHttpClient(string contentType)
        {
           var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc-4 asp.net-web-api dotnet-httpclient iis-8

6
推荐指数
1
解决办法
9590
查看次数

C#网络凭据没有传递给服务器?

编辑:使用:

byte[] authBytes = System.Text.Encoding.UTF8.GetBytes(user + ":" + password);
wr.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
Run Code Online (Sandbox Code Playgroud)

似乎工作正常.

我有一个与CMS通信的应用程序.我目前正在尝试让这个客户端能够使用"POST"方法将文本/ xml数据上传到CMS.

我可以通过使用curl完美地传递这个:

curl -u user:password -H "Content-Type:text/xml" -d "<element>myXML</element>" serverURL
Run Code Online (Sandbox Code Playgroud)

但是,尝试在C#中使用HttpWebRequest我无法让服务器返回我想要的内容.所以我解雇了Wireshark并查看了实际传递的内容,它几乎完全相同,除了使用curl时我可以看到:

Authorization: Basic <a bunch of hex>=\r\n
Credentials: user:password
Run Code Online (Sandbox Code Playgroud)

在HTTP标头字段中,在我的客户端的输出中,这些标头字段根本不存在.("凭据:"实际上并不是纯文本,它是"授权:"的子树 - 所以我不知道它从何处获取,但用户名和密码是正确的.)

我试图用来设置webrequest凭据的C#代码是这样的:

NetworkCredential myCred = new NetworkCredential(
                    user, password, serverURL);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(serverURL), "Basic", myCred);

HttpWebRequest wr = (HttpWebRequest) HttpWebRequest.Create(serverURL);
wr.Credentials = myCache;
Run Code Online (Sandbox Code Playgroud)

我也试过像这样设置凭据(并且没有指定serverURL):

wr.Credentials = new NetworkCredential(user,password,serverURL);
Run Code Online (Sandbox Code Playgroud)

但这仍然没有让它出现在wireshark中.有没有人知道如果:

A)授权信息实际上应该在HTTP标头中,以便工作,以及

B)如果是 - 那么我如何让C#把它放进去?我似乎只能使用默认凭据找到合适的示例,这不适用于我正在做的事情.

提前致谢.

c# httpwebrequest

3
推荐指数
1
解决办法
5252
查看次数