I want to connect to another server using network.
So I write below code.
var webRequest = WebRequest.Create(#"10.3.4.56");
using (var response = webRequest.GetResponse())
{
using (var rd = new StreamReader(response.GetResponseStream()))
{
var soapResult = rd.ReadToEnd();
}
}
But there is error and it says
Invalid URI: The format of the URI could not be determined.
How to solve it?
The string you pass in to WebRequest.Create needs to be a valid Uri. Try WebRequest.Create("http://10.3.4.56").
TIP! Use the static Uri.IsWellFormedUriString method to check if the URI string is valid.
try including the shema
"http://10.3.4.56"
Related
I want to get file list from owncloud on my ASP site. I was succeed using
curl -X PROPFIND -u user:password "http://yourserver.com/owncloud/remote.php/webdav/" from linux but I can't get the same result using default http request with propfind type in order to use it then in c# https://user:password#host/owncloud/remote.php/webdav. I get 400 code as a result on my request. Also I tried webdavclient from nuget but received method not allowed exception.
IClient c = new Client(new NetworkCredential { UserName = "user", Password = "password" });
var client = new WebDAVClient.Client(new NetworkCredential());
c.Server = "xxx.com/owncloud/remote.php/webdav/";
var isfolderCreated = c.CreateDir("/", "lalala").Result;
Could anybody say to me how to send http request to owncloud to get the file list? I tried webdav protocol that is used by clients but maybe I should try anything else?
I found the issue that prevented me. I just didn't use basic authorization correctly in http request. Since I add correct credentials I could send the http request and get the response. And here is the code I use in c#:
var request = (HttpWebRequest)WebRequest.Create("xxx.com/owncloud/remote.php/webdav/");
request.Credentials = new NetworkCredential("user", "password");
request.PreAuthenticate = true;
request.Method = #"PROPFIND";
request.Headers.Add(#"Translate", "F");
var httpGetResponse = (HttpWebResponse)request.GetResponse();
using (var responseStream = httpGetResponse.GetResponseStream())
{
long responseLength = httpGetResponse.ContentLength;
using (var streamReader = new StreamReader(responseStream, Encoding.UTF8))
{
var files = XElement.Parse(streamReader.ReadToEnd());
streamReader.Close();
}
responseStream.Close();
}
I'm currently working on getting course offerings from org unit id using c#.
I'm brand-new to D2L valence. I have app ID/key pair and user ID/Key pair.
I am going to enter org unit id, get json response, parse the json response in c#, and output the associated course code and name.
string GET(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}
}
this is my GET code. And, I'm trying to call it. The url and the main code are the following:
string url = "http://test.ca/d2l/api/lp/1.0/courses/644849";
GET(url);
The problem is I'm getting error says: The remote server returned an error: (403)Forbidden.
Also, I've tried this url:
string url = "http://lms.valence.desire2learn.com/d2l/api/lp/1.0/courses/644849";
This time, I got this error (Object reference not set to an instance of an object.)
I have app id/key pair and user id/key pair.
What should I do to solve this problem and end up with getting course offerings.
Thanks in advance, Phillip
The reason you're getting a 403 forbidden is because you are not sending the appropriate identifiers and signatures on your query string to allow your request to be authenticated (see http://docs.valence.desire2learn.com/basic/auth.html#id4).
If you are using C#, I'd recommend using the Valence SDK located on Nuget to generate appropriate URLs. Take a look at https://github.com/Brightspace/valence-sdk-dotnet/tree/master/samples/Basic for a sample project using the SDK, specifically, https://github.com/Brightspace/valence-sdk-dotnet/blob/master/samples/Basic/Basic/Controllers/HomeController.cs, shows how you can use the SDK methods to make a whoami request.
I got a string that represent a url and i want to download the content via c#
my url contains right to left mark and left to right mark %E2%80%8F & %E2%80%8E.
when i paste the url in the browser i can show the file.
when i use the code in .net i get an error ( because .net ignore those marks and doesnt send them in the request.
this is the query string i got
/MBA%20%281%29/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20%28%E2%80%8F%EF%BB%BF13015%E2%80%8E%29%E2%80%8F/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf
using fiddle i can that .net send
/MBA%20(1)/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20(%EF%BB%BF13015)/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf
youll notice that marks are gone.
any idea how to solve it?
here some code:
var s = "https://dl.dropboxusercontent.com/1/view/4fsaouo0ob52xkz/MBA%20%281%29/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20%28%E2%80%8F%EF%BB%BF13015%E2%80%8E%29%E2%80%8F/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf";
using (HttpClient client = new HttpClient())
{
using (var sr = client.GetAsync(s).Result)
{
Console.WriteLine(sr.Headers);
}
}
I tried to create the uri manually and pass it to the httpClient - save result.
The sr - got 403 error code instead of 200( the link i provided in invalid but the end result is the same).
Try this:
var uri = "https://dl.dropboxusercontent.com/1/view/4fsaouo0ob52xkz/MBA%20%281%29/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%A2%D7%A1%D7%A7%D7%99%D7%AA%20%D7%AA%D7%97%D7%A8%D7%95%D7%AA%D7%99%D7%AA%20%28%E2%80%8F%EF%BB%BF13015%E2%80%8E%29%E2%80%8F/%D7%93%D7%A4%D7%99%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA%20%D7%9C%D7%9E%D7%91%D7%97%D7%9F/%D7%90%D7%A1%D7%98%D7%A8%D7%98%D7%92%D7%99%D7%94%20%D7%93%D7%A3%20%D7%A0%D7%95%D7%A1%D7%97%D7%90%D7%95%D7%AA-2%20%D7%A2%D7%9E-%203%20%D7%A2%D7%9E%D7%95%D7%93%D7%95%D7%AA%20%D7%9C%D7%A8%D7%95%D7%97%D7%91.pdf";
string Url= System.Web.HttpUtility.UrlDecode(uri);
//objDataToXml.GenerateXml();
using (HttpClient client = new HttpClient())
{
using (var sr = client.GetAsync(Url).Result)
{
Console.WriteLine(sr.Headers);
}
}
I am facing an exception while using Google translation API V2. Exception text is "The remote server returned an error: (403) Forbidden". Exception occurs when req.GetResponse()function called. I am using following code. Please mention if any correct code is available.
Thanks
public static string Translate()
{
String textToTranslate = "Common";
String fromLanguage = "en"; // english
String toLanguage = "ur"; // spanish
String apiKey = /*My API Key*/;
// create the url for making web request
String apiUrl = "https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&q={3}";
String url = String.Format(apiUrl, apiKey, fromLanguage, toLanguage, textToTranslate);
string text = string.Empty;
try
{
// create the web request
WebRequest req = HttpWebRequest.Create(url);
// set the request method
req.Method = "GET";
// get the response
using (WebResponse res = req.GetResponse())
{
// read response stream
// you must specify the encoding as UTF8
// because google returns the response in UTF8 format
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
{
// read text from response stream
text = sr.ReadToEnd();
}
}
}
catch (Exception e)
{
throw; // throw the exception as is/
}
// return text to callee
return text;
}
You either run into some Google-set API usage limit (see http://code.google.com/apis/language/translate/v2/getting_started.html)
OR
The problem lies in the language (ur = Urdu ?) you are using... you should check whether this combination is actually available via the respective API. If you really want spanish as your comment suggests I suspect that would be es.
Another point:
You are not escaping your URL parameters (esp. the text to be translated) which in turn could lead to some problems in the future...
I find many example how use Privoxy/TOR for proxy. For example: How to use Tor to make a C# HttpWebRequest
First I installed Vidalia Bundle and than also Privoxy.
Vidalia Bundle using address 127.0.0.1:9115
Privoxy using address 127.0.0.1:8118
I try in code create request on server http://whatismyipaddress.com/.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://whatismyipaddress.com/");
request.Proxy = new WebProxy("127.0.0.1:8118");
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
webBrowser1.DocumentText = reader.ReadToEnd();
}
}
But this server still see my IP address. What I am doing wrong ? Any advance, thank you.
Edit, with leppie advice:
I use this constructor :
request.Proxy = new WebProxy("127.0.0.1",8118);
But server still see my IP adress. :(
Application is using Privoxy on port 8118. I need foward on 9115-this is TOR port.
I suspect the url is wrong.
You should probably use the WebProxy(string Host, int Port) constructor.
This works for a remote proxy:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://whatismyipaddress.com/");
request.Proxy = new WebProxy("110.139.166.78:8080");
using (var req = request.GetResponse())
{
using (StreamReader reader = new StreamReader(req.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
Console.ReadLine();