Ok, maybe this is the dumbest question ever but I really can't understand what's happening :D
I have this simple code:
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri url = new Uri("http://www.something.com/");
WebClient wc = new WebClient();
wc.DownloadStringAsync(url);
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
}
It works well, except for one case (obviously the one that I need), when the address of the server is "ts4.travian.it". In this case I get this error: "The remote server returned an error: NotFound."
The strange thing is that if I write a small console application with basically the same code, it works...any idea?
EDIT: To be more specific, the server return error 403 - Forbidden, but if I try the same code in a Console Application, it works perfectly...don't know what to think..I'm debugging the application on a Nokia Lumia 800
I found the problem finally. The Silverlight framework automatically set the Referer header of the HttpWebRequest, and the server "ts4.travian.it" refused it because it was not correct.
Did you forget to provide the output for that one case?
Edit:
Status code '403' implies that there' something the server doesn't like in your app. Who knows maybe it checks your UA string and sees that you're using IE Mobile i guess. Than he desides not to allow you see the content. Hm?
Related
So I want to make a chat application in C# using the NuGet module "SimpleTCP", I made it work so the clients can send messages to the server and all clients get those messages but what I want now is to make it so I could pass a name string in the client.WriteLine("Message") when I send a message to the server telling a message was sent("client" is my "SimpleTcpClient" variable).
The problem is that the "WriteLine()" function get only ONE argument so it can't get a Message and Name variables.
I have tried searching google for a solution for that but found nothing.
I also tried just doing "client.WriteLine("This is a message", "Tal M")" but it didn't work.
This is some of the code that tells the client to send a message to the server:
private void SendMessageInput_Click(object sender, EventArgs e)
{
client.WriteLine(MessageInput.Text);
}
This is the lines that makes the server get the message and broadcast it to all other(including the client that send the message) clients:
private void Server_DataReceived(object sender, SimpleTCP.Message e)
{
txtStatus.Invoke((MethodInvoker)delegate ()
{
txtStatus.Text += e.MessageString;
server.Broadcast(String.Format(e.MessageString));
});
}
What I expect is to be able to get a message that includes Two arguments; First one for a message(String), and Second one for a name(String). I would really appreciate getting help, Thanks.
So I found a solution for my problem! All I had to do is just format the String that I send so it would include both Name and Message using client.WriteLine(String.Format("{0}: {1}", "Bob", "Hello World")).
This solution isn't great since I might need to change it to actually get the Name and Message as variables. If I would want to do that I would need to send a JSON object which can work really well but I don't want to do it now(This solution was suggested by #Sir Rufo, Thanks for the help.
I've found several different questions about this error, but none of them seem to outline my scenario.
I am creating a website that pulls in tweets from our company's Twitter account, and displaying them on a social wall. I am using C# asp.NET webforms. The C# code uses a Linqtotwitter library to handle the authentication and the "tweet pulling." It grabs the tweets and dumps them onto an aspx file as a big long string of json. We then have a jquery script that reads through the json and displays the tweets on the page nice and pretty like.
The code currently works perfect on my dev box. But when I push the code up to production I get this .NET error:
The remote certificate is invalid according to the validation procedure
I'll provide my code in a bit here, but first let me give you a little background. I have no idea if this information would be relevant or not, but who knows. This website is actually part of a larger project to fit several tiny one page microsites that we get from marketing onto one server to reduce the overhead they cause. These microsites can all have a different host name, but they point to the same IP address. An httpmodule lives on that server, and intercepts all requests coming in, and redirects them to an appropriate sub folder depending on the host name.
From the research that I've done, it seems that SSL is tied into this error quite a bit. I'm still pretty new to the IT world, and I'm learning more about SSL as this troubleshooting goes on. The server these microsites live on does have a few SSL certificates on it, and one of the microsites uses SSL, but not the website I'm currently working on. But since they both share the same IP address in that sense they kind of ARE the same website.
This is the C# LinqtoTwitter code:
private SingleUserAuthorizer auth;
private TwitterContext twitterCtx;
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
auth = new SingleUserAuthorizer
{
Credentials = new SingleUserInMemoryCredentials
{
ConsumerKey =
ConfigurationManager.AppSettings["twitterConsumerKey"],
ConsumerSecret =
ConfigurationManager.AppSettings["twitterConsumerSecret"],
TwitterAccessToken =
ConfigurationManager.AppSettings["twitterAccessToken"],
TwitterAccessTokenSecret =
ConfigurationManager.AppSettings["twitterAccessTokenSecret"]
}
};
if (auth.IsAuthorized)
{
twitterCtx = new TwitterContext(auth);
var tweetResponse =
(from tweet in twitterCtx.Status
where tweet.Type == StatusType.User &&
tweet.ScreenName == "OurProfile" &&
tweet.IncludeRetweets == true
select tweet)
.ToList();
Results.Text = twitterCtx.RawResult;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (twitterCtx != null)
{
twitterCtx.Dispose();
twitterCtx = null;
}
}
Does anyone have any ideas of what could be the problem here? Like I said, I'm still pretty new, and I'm at a loss here even how to troubleshoot this issue beyond Google. Could it be something where our server can't verify that Twitter's SSL certificate is from a trusted source? Let me know if I can provide any more information or any more code. Thanks for your time and for reading through my post!
I need to use a JSON REST Service inside my Silverlight Application so that I can display information from the REST service in my Silverlight App. Below is my code but the ClientDownloadStringCompleted() is never stepped into and I'm not sure why. The REST service does have data and is working. I just can't seem to access it so I can parse out the data I need. View of the JSON Service
string jsonlink = "http://hou-prodserver2/ArcGIS/rest/services/sar/ScheduledRig/MapServer/81/query?text=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=MAXDATE%3E%271%2F1%2F1900%27&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=false&maxAllowableOffset=&outSR=&outFields=MAXDATE&f=pjson";
var client = new WebClient();
client.DownloadStringCompleted += ClientDownloadStringCompleted;
client.DownloadStringAsync(new Uri(jsonlink, UriKind.Absolute));
Below is Method I'm trying to use to get access to the data but it never gets used
private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//handle the response.
string result = e.Result;
}
Make sure that service site has one or both of the following files at the root of the site:
crossdomain.xml
clientaccesspolicy.xml
If your Silverlight app is not on the same exact domain as the service, those files need to exist to allow it to connect. If they don't Silverlight fails silently.
I'm writing a small utility that scrapes a webpage and emails me if the class I want has open seats. See the follow url:
Math Classes
However, I get an error everytime I execute the following code:
private void timer1_Tick(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
//var html = client.DownloadString("https://grim.westga.edu/pls/UWGP_DAD/hwwkbull.p_us_subj_list?term_code=201108&subj=math&levl=US");
client.DownloadFile(#"https://grim.westga.edu/pls/UWGP_DAD/hwwkbull.p_us_subj_list?term_code=201108&subj=math&levl=US", #"C:\file.txt");
}
}
Any idea?
That link you posted also supports simple HTTP.
See here: http://grim.westga.edu/pls/UWGP_DAD/hwwkbull.p_us_subj_list?term_code=201108&subj=math&levl=US
Try removing your s from the url and see if that works.
What is the preferred way to open a URL from a thick client application on Windows using C# and the .NET framework? I want it to use the default browser.
The following code surely works:
Process.Start("http://www.yoururl.com/Blah.aspx");
It opens the default browser (technically, the default program that handles HTTP URIs).
I'd use the Process.Start method.
private void launchURL_Click(object sender, System.EventArgs e){
string targetURL = "http://stackoverflow.com";
System.Diagnostics.Process.Start(targetURL);
}
System.Diagnostics.Process.Start("http://www.stackoverflow.com");