How to extract Digg data by Digg API - extract

I am trying to extract Digg data for a user using this URL:
"http://services.digg.com/user/vamsivanka/diggs?count=25&appkey=34asd56asdf789as87df65s4fas6"
and the web response is throwing an error "The remote server returned an error: (403) Forbidden."
Please let me know.
public static XmlTextReader CreateWebRequest(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = ".NET Framework digg Test Client";
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Accept = "text/xml";
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
System.IO.Stream responseStream = webResponse.GetResponseStream();
XmlTextReader reader = new XmlTextReader(responseStream);
return reader;
}

Related

RestSharp: Stackoverflow exception at Client.Execute

Hoping you'd be able to assist me with this error - I am getting
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
at the time when Client.Execute is called, I've shared my code below:
Thanks in Advance!
var Client = new RestClient();
Client.BaseUrl = new Uri("http://dummyurl.com/");
Client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("username", "password");
var eventlist = new List<string>();
var request = new RestRequest(Method.POST);
request.AddHeader("event", "application/json");
string jsonBody = #"{'xxxxx':'xxxx',
'xxxx':'Xxxxx'}";
JObject jsonObject = JObject.Parse(jsonBody);
request.AddBody(jsonObject);
IRestResponse response = Client.Execute(request); //<<<--Throws Stackoverflow exception
var content = JsonConvert.DeserializeObject(response.Content);
It blows up on serialising the JObject instance. You don't need to overload your code with repeated serialization and JSON parsing.
To pass a ready-made JSON as the request body, just use AddParamerter(contentType, jsonString, ParameterType.RequestBody);
Set the content type accordingly to application/json or whatever your server wants.

How to send multiple requests via Postman

I know we can send a json object via Postman as a POST request. However, I wish to send multiple such json Objects(hundreds) consecutively via. Postman.
Is there some way I can achieve that? I am stuck and will highly appreciate some solutions.
TIA:)
So I ended up sending the json data via. a java code through POST requests.
Since I needed this only for load testing, I didn't care much about the code efficiency here. Might be useful for someone
String line;
JSONParser jsonParser = new JSONParser();
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
while((line = br.readLine()) != null) {
JSONObject jsonObject = (JSONObject) jsonParser.parse(line);
HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("http://url");
StringEntity params = new StringEntity(jsonObject.toString());
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);.
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown();
}
}

WP8 Sending JSON to Server

I am trying to POST some JSON data to my Server. I am using simple code which I have attained from: Http Post for Windows Phone 8
Code:
string url = "myserver.com/path/to/my/post";
// HTTP web request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/plain; charset=utf-8";
httpWebRequest.Method = "POST";
// Write the request Asynchronously
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
httpWebRequest.EndGetRequestStream, null))
{
//create some json string
string json = "{ \"my\" : \"json\" }";
// convert json to byte array
byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);
// Write the bytes to the stream
await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
}
I am getting an error though on await and Task:
Anyone see the obvious error?
Fixed it by changing method signature to:
public async Task ReportSighting(Sighting sighting)

Box Api :: The remote server returned an error: (401) Unauthorized

I am getting the error
The remote server returned an error: (401) Unauthorized
Here is the the code.
String sFolderURL = #"https://www.box.com/api/";
string Headers = string.Format("BoxAuth api_key={0}&auth_token={1}", api_key, authToken);
String sParam = "2.0/folders/0";
String sURL = sFolderURL + sParam;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
request.Method = "POST";
request.Headers.Add("Authorization", Headers);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
A 401 is only returned in the case of sending an expired/invalid access_token. You probably need to either
Refresh the token you have
Go through the auth process from the start to get a valid token

json parsing in blackberry?

I am working on a web-service application using JSON.
In performing task I got success in directly fetching JSOn response by hitting the URL.
Now I have a task to request with a request parameter.
enter code here
private void callJSON_Webservice(String method,String paraLastModifiedDate) {
HttpConnection c=null;
InputStream is = null;
String feedURL = Constants.feedURL;
int rc;
try{
JSONObject postObject = new JSONObject();
postObject.put("CheckLatestDataDate",method);
postObject.put("LastModifiedDate", paraLastModifiedDate);
//c = new HttpConnectionFactory().getHttpConnection(feedURL);
c = (HttpConnection)Connector.open(feedURL + ConnectionManager.getConnectionString());
// Set the request method and headers
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
c.setRequestProperty("Content-Length", "" + (postObject.toString().length() - 2));
//c.setRequestProperty("method", HttpConnection.GET);
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK){
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
String json = StringUtils.convertStreamToString(is);
object = new JSONObject(json);
}catch (Exception e) {
System.out.println(e+"call webservice exception");
}
}
With this code I am getting EOF exception. I need to complete this small task as soon as possible. Please help me...!
Thanx in advance
Try replacing
is = c.openInputStream();
String json = StringUtils.convertStreamToString(is);
with following:
is = c.openInputStream();
StringBuffer buffer = new StringBuffer();
int ch = 0;
while (ch != -1) {
ch = is.read();
buffer.append((char) ch);
}
String json = buffer.toString();
Reference: convert StreamConnection to String