提问人:FALGUNI BHAVSAR 提问时间:7/13/2023 最后编辑:FALGUNI BHAVSAR 更新时间:7/19/2023 访问量:1062
System.Net.WebException:试图以套接字的访问权限禁止的方式访问套接字
System.Net.WebException: An attempt was made to access a socket in a way forbidden by its access permissions
问:
var uploadImagesURL = "https://localhost:57028/Photo/UploadImages?albumId="+ albumId;
cookieString = parsedFormBody.Result.GetParameterValue("Cookie");
HttpWebRequest uploadImageWebRequest = (HttpWebRequest)WebRequest.Create(uploadImagesURL);
uploadImageWebRequest.Method = "POST";
var cookies = new CookieContainer();
string[] cookiePairs = cookieString.Split(';');
foreach (string cookiePair in cookiePairs)
{
string[] keyValue = cookiePair.Trim().Split('=');
if (keyValue.Length == 2)
{
string cookieName = keyValue[0];
string cookieValue = keyValue[1];
var cookie = new Cookie(cookieName, cookieValue)
{
Domain = "localhost"
};
cookies.Add(cookie);
}
}
uploadImageWebRequest.CookieContainer = cookies;
HttpWebResponse uploadImageWebResponse = (HttpWebResponse)uploadImageWebRequest.GetResponse();
我创建了一个 Azure 函数,用于上传多个图像的可伸缩性,并且从 ajax 请求中,我正在调用我的 azure 函数,从 azure 函数,我正在上传到 wasabi,当图像成功上传时,我正在调用 HttpWebRequest,但在步骤 (HttpWebResponse)uploadImageWebRequest.GetResponse();它导致错误 - System.Net.WebException:尝试以访问权限禁止的方式访问套接字
答:
0赞
Pavan
7/19/2023
#1
错误 - System.Net.WebException:尝试以套接字访问权限禁止的方式访问套接字
- 大多数情况下,这些问题都与SSL证书或安全限制有关。如果在本地运行应用程序,请尝试使用 而不是
http://localhost:57028
https://localhost:57028
我尝试过一个示例 Web API,其中 HttpWebRequest 会将名称和值发布到 cookie。请检查以下代码。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace WebApplication2
{
class WebApplication2
{
static void Main()
{
try
{
string url = "https://jsonplaceholder.typicode.com";
string postData = "{\"title\":\"New Post\",\"body\":\"This is the post body\",\"userId\":1}";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
// Convert the POST data to a byte array
byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
request.ContentLength = postDataBytes.Length;
// Add cookies to the request
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Cookie("session_id", "abc123", "/", "jsonplaceholder.typicode.com"));
request.CookieContainer.Add(new Cookie("auth_token", "xyz789", "/", "jsonplaceholder.typicode.com"));
// Write the POST data to the request stream
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(postDataBytes, 0, postDataBytes.Length);
}
// Send the request and get the response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Read the response stream using a StreamReader
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
Console.WriteLine(responseText);
}
}
}
catch (WebException ex)
{
if (ex.Response is HttpWebResponse response)
{
Console.WriteLine("HTTP error: " + response.StatusCode);
}
else
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
}
- 我在上面的代码中添加了 try-catch 方法来处理发送 Web 请求时发生的异常。
结果:
评论
HttpClient
WebRequest
+ memoryStream