I am trying to make SignalR Client work from C# code but I couldn't. I got this error:
"StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
Access-Control-Allow-Origin: *
Cache-Control: private
Date: Tue, 30 Jul 2019 11:54:07 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Type: text/html; charset=utf-8
}"
But it works fine from jQuery. This is my code:
I tried this tutorial : https://programmer.group/signalr-hubs-api-details-net-c-client.html and also at the server side this my startup file code
var config = new HubConfiguration();
config.EnableDetailedErrors = true;
config.EnableJSONP = true;
app.MapSignalR(config);
And this in the Client side:
async void ConnectToSignlr()
{
try
{
HubConnection hubConnection = new HubConnection("http://localhost:8087/");
hubConnection.Credentials = System.Net.CredentialCache.DefaultCredentials;
hubConnection.TraceLevel = TraceLevels.All;
hubConnection.TraceWriter = Console.Out;
IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub");
proxy.On<string>("chekroomstauts", (message) =>
{
//do something on your ui maybe?
Response.Write("hello you connected");
});
proxy.On("notify", (message) =>
{
Response.Write("hello you notify");
}
);
// connect to hup pro
await hubConnection.Start();
await proxy.Invoke("Join", "1000");
}
catch (AggregateException ex)
{
foreach (var errInner in ex.InnerExceptions)
{
//Debug.WriteLine(errInner); //this will call ToString() on the inner execption and get you message, stacktrace and you could perhaps drill down further into the inner exception of it if necessary
Response.Write(errInner + "</br>");
}
}
}
Without the exception detials I can only guess.
Maybe "chekroomstauts" is a typo and your server wants to call "checkroomstatus"?
Maybe "Join" is not defined?
Kind regards
Bernd
Related
I am trying to call a POST method of Moosend API from my MVC application. The Post call is working fine from Postman but throwing error from controller.
The Code goes as below:
public async Task<ActionResult> CreateEmailList()
{
try
{
using (var httpClient = new HttpClient { BaseAddress = new Uri(Baseurl) })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
using (var content = new StringContent("{ \"Name\": \"APIList\"}", System.Text.Encoding.Default, "application/json"))
{
using (var response = await httpClient.PostAsync("Mailing Lists/lists/create.json?apikey=0000", content))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
return View();
}
}
catch (Exception ex)
{
return View();
}
}
The Error I am getting is as below:
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
X-Robots-Tag: noindex, nofollow
X-Server-ID: 2
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, Accept, Cache-Control, X-Requested-With
Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT
Cache-Control: private
Date: Thu, 25 Nov 2021 06:58:44 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 5054
Content-Type: text/html; charset=utf-8
}}
Any suggestions on what am I missing here. Most of the POST example is similar to the code that I implemented.
INTERNAL SERVER ERRRO IN ASP.NET MVC | Im using API Nodejs API
I'm getting this error :
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Origin: *
Connection: keep-alive
Keep-Alive: timeout=5
Date: Thu, 10 Jun 2021 10:43:08 GMT
ETag: W/"207-dot71BoPtBULXh2HngHqO05/OVY"
X-Powered-By: Express
Content-Length: 519
Content-Type: application/json; charset=utf-8
}}
[HttpPost]
public ActionResult Create(UserViewModel model)
{
try
{
string data = JsonConvert.SerializeObject(model);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(client.BaseAddress + "users/registration", content).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
return View();
}
catch (Exception ex)
{
throw;
}
}
I have tried every blog I could find about it . But I am not able to resolve status 401 with NTLM challenge.
So basically I need to get profile picture of an user which is hosted in company server.
When I hit the url in browser It prompts for username and password ,once I provide it I get the image.
But same I am not able to achieve in Xamarin.iOS
Below is the code snippet I am trying with .
var credentials = new NetworkCredential("myUserName", "Password", "domain");
var handler = new HttpClientHandler { Credentials = credentials, UseDefaultCredentials = false };
handler.AllowAutoRedirect = false;
var client = new HttpClient(handler);
try
{
var response = await client.GetAsync("https://mycustomUrl.net/User%20Photos/Profile%20Pictures/eurn_martin_kremmer_MThumb.jpg");
Console.WriteLine(response);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Response I am getting is
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Microsoft-IIS/8.0
SPRequestGuid: 5725369e-b047-40c5-d468-0417992ffab3
request-id: 5725369e-b047-40c5-d468-0417992ffab3
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 2
SPIisLatency: 0
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4779
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Wed, 13 Dec 2017 13:55:26 GMT
Content-Length: 0
}
Wow , I didn't know the solution is as easy as pie , when you throw the same request with NSURLConnection.
here,
responseData = new NSMutableData();
request = new NSMutableUrlRequest(uri,NSUrlRequestCachePolicy.UseProtocolCachePolicy, 6000);
request.HttpMethod = "GET";
connection = new NSUrlConnection(request, this);
This works without hassle in Citrix MAM environment.
But to test it in simulator you need to handle challenge like below.
public override void WillSendRequestForAuthenticationChallenge(NSUrlConnection connection, NSUrlAuthenticationChallenge challenge)
{
Console.WriteLine(challenge.ProtectionSpace);
if (challenge.PreviousFailureCount == 0 )
{
NSUrlCredential cred = new NSUrlCredential(this.myUserName, this.myPass, NSUrlCredentialPersistence.None);
challenge.Sender.UseCredential(cred, challenge);
}
}
I want to have a possibility to return complex response for requests in Web API. For example I want to return stream with some complex object.
I tried to do it with MultipartFormDataContent:
public static HttpResponseMessage GetMultipartResponse<T>(T responseData, Stream streamToReturn)
{
return new HttpResponseMessage
{
Content = new MultipartContent
{
new StreamContent(streamToReturn),
new ObjectContent<T>(responseData, new JsonMediaTypeFormatter())
}
};
}
I've got a normal HttpResponseMessage on server side. I can see by debugger how my Web API method returns this response, but on the client side I've got an error:
StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNccnVzdGFtX3NhbGFraHV0ZGlub3ZcZG9jdW1lbnRzXHZpc3VhbCBzdHVkaW8gMjAxM1xQcm9qZWN0c1xGaWxlU2VydmljZUFQSVxGaWxlU2VydmljZUFQSVxhcGlcZ2V0?=
Cache-Control: no-cache
Date: Tue, 26 Jul 2016 08:30:22 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 1444
Content-Type: application/json; charset=utf-8
Expires: -1
}
There is no other exceptions on client or server side and i can't get some more information with turn on error detail:
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
UPDATE:
Try to read response on client side with:
var result = await response.Content.ReadAsMultipartAsync();
And got this exception:
Invalid 'HttpContent' instance provided. It does not have a content type header starting with 'multipart/'.
Where do I go wrong?
UPDATE 2:
I have normal response on the server side:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.MultipartContent, Headers:
{
Content-Type: multipart/multipart; boundary="3e6d3573-9031-41a7-b7f4-d1421bc1451d"
}
I found the error. I return my multipart content inside using:
using (var stream = MyFileStream())
{
var respone = MultipartResponseHelper.GetMultipartResponse(response, stream, Request);
return respone;
}
So it returns right response, but then stream inside multipartContent will disposed which is the reason of problem.
So I'm trying pass user data from one controller to another. I've been reading a lot about it here ---> http://www.dotnet-tricks.com/Tutorial/webapi/F2aL081113-Passing-multiple-complex-type-parameters-to-ASP.NET-Web-API.html and this guy seems to be doing exactly what I want to do, and he's using .PostAsJsonAsync to do it.
This is controller 1, which takes in user data and basically re-routes it to the other controller for authentication.
public ActionResult IDB(IDBUser i)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:64819");
HttpResponseMessage result = client.PostAsJsonAsync("/api/Participant/Authenticate", i).Result;
if (result.IsSuccessStatusCode)
{
return View();
}
else { // hits this breakpoint with 500 server error
return View("Index");
}
}
}
}
This seems to successfully make it to the other controller (below), as I'm getting a 500 error. However, I've got a breakpoint right at the beginning of the method, and it never seems to hit it.
[System.Web.Http.ActionName("Authenticate")]
[System.Web.Http.HttpPost]
public HttpResponseMessage Authenticate(IDBUser u)
{ //breakpoint it neve hits is on this line
bool Authenticate = false;
CredentialTester ct = new CredentialTester(u.password, u.username);
bool isAuthenticatedInt = ct.IntTest();
bool isAuthenticatedAcp = ct.AcpTest();
if (isAuthenticatedInt == true || isAuthenticatedAcp == true)
{
Authenticate = true;
return Request.CreateResponse(HttpStatusCode.OK, Authenticate);
}
else Authenticate = false;
return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}
Any help is apprecitaed. Thanks everyone!
UPDATE
: I was able to look at the error and it is listed here:
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Pragma: no-cache X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcYTU4NDYxOVxEb2N1bWVudHNcYXAxMDk1MTQtcWEtYXVvdG1hdGlvblxXaVFhLlZhbC1JZFxNVkNcV2lRYS5WYWwtSWQuV2ViU2VydmljZVxXaVFhLlZhbC1JZC5XZWJTZXJ2aWNlXFdpUWEuVmFsLUlkLldlYlNlcnZpY2VcYXBpXFBhcnRpY2lwYW50XEF1dGhlbnRpY2F0ZQ==?= Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Content-Type, Accept Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Cache-Control: no-cache Date: Thu, 12 May 2016 17:07:39 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 2307 Content-Type: application/json; charset=utf-8 Expires: -1}}
it looks like there's a problem with my Access- Control-Origin. I've put the appropriate adjustments in my web.config file (I've had a similar error before) but no luck.